more stuff
[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 % Unicode
10 \usepackage{ucs}
11 \usepackage[utf8x]{inputenc}
12 % \usepackage{autofe}
13
14 %% -----------------------------------------------------------------------------
15 %% Fonts
16 %% \usepackage[osf]{libertine}
17 %% \usepackage{helvet}
18 %% \usepackage{lmodern}
19 %% \IfFileExists{microtype.sty}{\usepackage{microtype}}{}
20
21 %% -----------------------------------------------------------------------------
22 %% Diagrams
23 % \usepackage{tikz}
24 % \usetikzlibrary{positioning}
25 %% \usetikzlibrary{shapes}
26 %% \usetikzlibrary{arrows}
27 %% \usepackage{tikz-cd}
28 %% \usepackage{pgfplots}
29 \usepackage[all]{xy}
30
31 %% -----------------------------------------------------------------------------
32 %% Symbols
33 \usepackage[fleqn]{amsmath}
34 \usepackage{amssymb}
35 \usepackage{wasysym}
36 \usepackage{turnstile}
37 \usepackage{centernot}
38
39 %% -----------------------------------------------------------------------------
40 %% Utils
41 \usepackage{bussproofs}
42 \usepackage{umoline}
43 \usepackage[export]{adjustbox}
44 \usepackage{multicol}
45 \usepackage{listings}
46 \lstset{
47   basicstyle=\footnotesize\ttfamily,
48   extendedchars=\true,
49   inputencoding=utf8x
50 }
51
52 %% -----------------------------------------------------------------------------
53 %% Bibtex
54 \usepackage{natbib}
55
56 %% Numbering depth
57 %% \setcounter{secnumdepth}{0}
58
59 %% -----------------------------------------------------------------------------
60 % We will generate all images so they have a width \maxwidth. This means
61 % that they will get their normal width if they fit onto the page, but
62 % are scaled down if they would overflow the margins.
63 \makeatletter
64 \def\maxwidth{
65   \ifdim\Gin@nat@width>\linewidth\linewidth
66   \else\Gin@nat@width\fi
67 }
68 \makeatother
69
70 %% -----------------------------------------------------------------------------
71 %% Links
72 \usepackage[
73   setpagesize=false, % page size defined by xetex
74   unicode=false, % unicode breaks when used with xetex
75   xetex
76 ]{hyperref}
77
78 \hypersetup{
79   breaklinks=true,
80   bookmarks=true,
81   pdfauthor={Francesco Mazzoli <fm2209@ic.ac.uk>},
82   pdftitle={The Paths Towards Observational Equality},
83   colorlinks=false,
84   pdfborder={0 0 0}
85 }
86
87 % Make links footnotes instead of hotlinks:
88 % \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
89
90 % avoid problems with \sout in headers with hyperref:
91 \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
92
93 \title{The Paths Towards Observational Equality}
94 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
95 \date{December 2012}
96
97 \begin{document}
98
99 \maketitle
100
101 \setlength{\tabcolsep}{12pt}
102
103 The marriage between programming and logic has been a very fertile one.  In
104 particular, since the simply typed lambda calculus (STLC), a number of type
105 systems have been devised with increasing expressive power.
106
107 In the next sections I will give a very brief overview of STLC, and then
108 describe how to augment it to reach the theory I am interested in,
109 Inutitionistic Type Theory (ITT), also known as Martin-L\"{o}f Type Theory after
110 its inventor.  The exposition is quite dense since there is a lot of material to
111 cover, for a more complete treatment of the material the reader can refer to
112 \citep{Thompson1991, Pierce2002}.
113
114 I will then explain why equality has been a tricky business in these theories,
115 and talk about the various attempts have been made to make the situation better.
116 One interesting development has recently emerged: Observational Type theory.  I
117 propose to explore the ways to turn these ideas into useful practices for
118 programming and theorem proving.
119
120 \section{Simple and not-so-simple types}
121
122 \subsection{Untyped $\lambda$-calculus}
123
124 Along with Turing's machines, the earliest attempts to formalise computation
125 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
126 language encodes computation with a minimal sintax and most notably no `data'
127 in the traditional sense, but just functions.
128
129 The syntax of $\lambda$-terms consists of just three things: variables,
130 abstractions, and applications:
131
132 \newcommand{\appsp}{\hspace{0.07cm}}
133 \newcommand{\app}[2]{#1\appsp#2}
134 \newcommand{\absspace}{\hspace{0.03cm}}
135 \newcommand{\abs}[2]{\lambda #1\absspace.\absspace#2}
136 \newcommand{\termt}{t}
137 \newcommand{\termm}{m}
138 \newcommand{\termn}{n}
139 \newcommand{\termp}{p}
140 \newcommand{\termf}{f}
141 \newcommand{\separ}{\ \ |\ \ }
142 \newcommand{\termsyn}{\mathit{term}}
143 \newcommand{\axname}[1]{\textbf{#1}}
144 \newcommand{\axdesc}[2]{\axname{#1} \fbox{$#2$}}
145 \newcommand{\lcsyn}[1]{\mathrm{\underline{#1}}}
146
147 \begin{center}
148 \axname{syntax}
149 $$
150 \begin{array}{rcl}
151   \termsyn & ::= & x \separ (\abs{x}{\termsyn}) \separ (\app{\termsyn}{\termsyn}) \\
152          x & \in & \text{Some enumerable set of symbols, e.g.}\ \{x, y, z, \dots , x_1, x_2, \dots\}
153 \end{array}
154 $$
155 \end{center}
156
157
158 % I will omit parethesis in the usual manner. %TODO explain how
159
160 I will use $\termt,\termm,\termn,\dots$ to indicate a generic term, and $x,y$
161 for variables.  I will also assume that all variable names in a term are unique
162 to avoid problems with name capturing.  Intuitively, abstractions
163 ($\abs{x}{\termt}$) introduce functions with a named parameter ($x$), and
164 applications ($\app{\termt}{\termm}$) apply a function ($\termt$) to an argument
165 ($\termm$).
166
167 The `applying' is more formally explained with a reduction rule:
168
169 \newcommand{\bred}{\leadsto}
170 \newcommand{\bredc}{\bred^*}
171
172 \begin{center}
173 \axdesc{reduction}{\termsyn \bred \termsyn}
174 $$\app{(\abs{x}{\termt})}{\termm} \bred \termt[\termm ]$$
175 \end{center}
176
177 Where $\termt[\termm ]$ expresses the operation that substitutes all
178 occurrences of $x$ with $\termm$ in $\termt$.  In the future, I will use
179 $[\termt]$ as an abbreviation for $[\termt ]$.  In the systems presented, the
180 $\bred$ relation also includes reduction of subterms, for example if $\termt
181 \bred \termm$ then $\app{\termt}{\termn} \bred \app{\termm}{\termn}$, and so on.
182
183 % % TODO put the trans closure
184
185 These few elements are of remarkable expressiveness, and in fact Turing
186 complete.  As a corollary, we must be able to devise a term that reduces forever
187 (`loops' in imperative terms):
188 \[
189   \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \dotsb
190 \]
191 Terms that can be reduced only a finite number of times (the non-looping ones)
192 are said to be \emph{normalising}, and the `final' term is called \emph{normal
193   form}.  These concepts (reduction and normal forms) will run through all the
194 material analysed.
195
196 \subsection{The simply typed $\lambda$-calculus}
197
198 \newcommand{\tya}{A}
199 \newcommand{\tyb}{B}
200 \newcommand{\tyc}{C}
201
202 One way to `discipline' $\lambda$-terms is to assign \emph{types} to them, and
203 then check that the terms that we are forming make sense given our typing rules
204 \citep{Curry1934}.
205
206 We wish to introduce rules of the form $\Gamma \vdash \termt : \tya$, which
207 reads `in context $\Gamma$, term $\termt$ has type $\tya$'.
208
209 The syntax for types is as follows:
210
211 \newcommand{\tyarr}{\to}
212 \newcommand{\tysyn}{\mathit{type}}
213 \newcommand{\ctxsyn}{\mathit{context}}
214 \newcommand{\emptyctx}{\cdot}
215
216 \begin{center}
217   \axname{syntax}
218    $$\tysyn ::= x \separ \tysyn \tyarr \tysyn$$
219 \end{center}
220
221 I will use $\tya,\tyb,\dots$ to indicate a generic type.
222
223 A context $\Gamma$ is a map from variables to types.  We use the notation
224 $\Gamma; x : \tya$ to augment it, and to `extract' pairs from it.
225
226 Predictably, $\tya \tyarr \tyb$ is the type of a function from $\tya$ to
227 $\tyb$.  We need to be able to decorate our abstractions with
228 types\footnote{Actually, we don't need to: computers can infer the right type
229   easily, but that is another story.}:
230 \begin{center}
231   \axname{syntax}
232    $$\termsyn ::= x \separ (\abs{x : \tysyn}{\termsyn}) \separ (\app{\termsyn}{\termsyn})$$
233 \end{center}
234 Now we are ready to give the typing judgements:
235
236 \begin{center}
237   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
238
239   \vspace{0.5cm}
240
241   \begin{tabular}{c c c}
242     \AxiomC{}
243     \UnaryInfC{$\Gamma; x : \tya \vdash x : \tya$}
244     \DisplayProof
245     &
246     \AxiomC{$\Gamma; x : \tya \vdash \termt : \tyb$}
247     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \tya \tyarr \tyb$}
248     \DisplayProof
249   \end{tabular}
250
251   \vspace{0.5cm}
252
253   \begin{tabular}{c}
254     \AxiomC{$\Gamma \vdash \termt : \tya \tyarr \tyb$}
255     \AxiomC{$\Gamma \vdash \termm : \tya$}
256     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb$}
257     \DisplayProof
258   \end{tabular}
259 \end{center}
260
261 This typing system takes the name of `simply typed lambda calculus' (STLC),
262 and enjoys a number of properties.  Two of them are expected in most type
263 systems: %TODO add credit to pierce
264 \begin{description}
265 \item[Progress] A well-typed term is not stuck - either it is a value or it can
266   take a step according to the evaluation rules.  With `value' we mean a term
267   whose subterms (including itself) don't appear to the left of the $\bred$
268   relation.
269 \item[Preservation] If a well-typed term takes a step of evaluation, then the
270   resulting term is also well typed.
271 \end{description}
272
273 However, STLC buys us much more: every well-typed term
274 is normalising.  It is easy to see that we can't fill the blanks if we want to
275 give types to the non-normalising term shown before:
276 \begin{equation*}
277   \app{(\abs{x : ?}{\app{x}{x}})}{(\abs{x : ?}{\app{x}{x}})}
278 \end{equation*}
279
280 \newcommand{\lcfix}[2]{\mathsf{fix} \appsp #1\absspace.\absspace #2}
281
282 This makes the STLC Turing incomplete.  We can recover the ability to loop by
283 adding a combinator that recurses:
284 $$
285   \termsyn ::= \dots \separ  \lcfix{x : \tysyn}{\termsyn}
286 $$
287 \begin{center}
288   \begin{prooftree}
289     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
290     \UnaryInfC{$\Gamma \vdash \lcfix{x : \tya}{\termt} : \tya$}
291   \end{prooftree}
292 \end{center}
293 $$
294   \lcfix{x : \tya}{\termt} \bred \termt[(\lcfix{x : \tya}{\termt}) ]
295 $$
296
297 However, we will keep STLC without such a facility. In the next section we shall
298 see why that is preferable for our needs.
299
300 \subsection{The Curry-Howard correspondence}
301 \label{sec:curry-howard}
302
303 \newcommand{\lcunit}{\mathsf{\langle\rangle}}
304
305 It turns out that the STLC can be seen a natural deduction system for
306 propositional logic.  Terms are proofs, and their types are the propositions
307 they prove.  This remarkable fact is known as the Curry-Howard correspondence,
308 or isomorphism.
309
310 The `arrow' ($\to$) type corresponds to implication.  If we wished to
311 prove that $(\tya \tyarr \tyb) \tyarr (\tyb \tyarr \tyc) \tyarr (\tya
312 \tyarr \tyc)$, all we need to do is to devise a $\lambda$-term that has the
313 correct type:
314 \begin{equation*}
315   \abs{f : (\tya \tyarr \tyb)}{\abs{g : (\tyb \tyarr \tyc)}{\abs{x : \tya}{\app{g}{(\app{f}{x})}}}}
316 \end{equation*}
317 That is, function composition.  We might want extend our bare lambda calculus
318 with a couple of terms to make our natural deduction more pleasant to use.  For
319 example, tagged unions (\texttt{Either} in Haskell) are disjunctions, and tuples
320 (or products) are conjunctions.  We also want to be able to express falsity, and
321 that is done by introducing a type inhabited by no terms.  If evidence of such a
322 type is presented, then we can derive any type, which expresses absurdity.
323 Conversely, $\top$ is the type with just one trivial element, $\lcunit$.
324
325 \newcommand{\lcinl}{\mathsf{inl}\appsp}
326 \newcommand{\lcinr}{\mathsf{inr}\appsp}
327 \newcommand{\lccase}[3]{\lcsyn{case}\appsp#1\appsp\lcsyn{of}\appsp#2\appsp#3}
328 \newcommand{\lcfst}{\mathsf{fst}\appsp}
329 \newcommand{\lcsnd}{\mathsf{snd}\appsp}
330 \newcommand{\orint}{\vee I_{1,2}}
331 \newcommand{\orintl}{\vee I_{1}}
332 \newcommand{\orintr}{\vee I_{2}}
333 \newcommand{\orel}{\vee E}
334 \newcommand{\andint}{\wedge I}
335 \newcommand{\andel}{\wedge E_{1,2}}
336 \newcommand{\botel}{\bot E}
337 \newcommand{\lcabsurd}{\mathsf{absurd}\appsp}
338 \newcommand{\lcabsurdd}[1]{\mathsf{absurd}_{#1}\appsp}
339
340
341 \begin{center}
342   \axname{syntax}
343   $$
344   \begin{array}{rcl}
345     \termsyn & ::= & \dots \\
346              &  |  & \lcinl \termsyn \separ \lcinr \termsyn \separ \lccase{\termsyn}{\termsyn}{\termsyn} \\
347              &  |  & (\termsyn , \termsyn) \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
348              &  |  & \lcunit \\
349     \tysyn & ::= & \dots \separ \tysyn \vee \tysyn \separ \tysyn \wedge \tysyn \separ \bot \separ \top
350   \end{array}
351   $$
352 \end{center}
353 \begin{center}
354   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
355   \begin{prooftree}
356     \AxiomC{$\Gamma \vdash \termt : \tya$}
357     \RightLabel{$\orint$}
358     \UnaryInfC{$\Gamma \vdash \lcinl \termt : \tya \vee \tyb$}
359     \noLine
360     \UnaryInfC{$\Gamma \vdash \lcinr \termt : \tyb \vee \tya$}
361   \end{prooftree}
362   \begin{prooftree}
363     \AxiomC{$\Gamma \vdash \termt : \tya \vee \tyb$}
364     \AxiomC{$\Gamma \vdash \termm : \tya \tyarr \tyc$}
365     \AxiomC{$\Gamma \vdash \termn : \tyb \tyarr \tyc$}
366     \RightLabel{$\orel$}
367     \TrinaryInfC{$\Gamma \vdash \lccase{\termt}{\termm}{\termn} : \tyc$}
368   \end{prooftree}
369
370   \begin{tabular}{c c}
371     \AxiomC{$\Gamma \vdash \termt : \tya$}
372     \AxiomC{$\Gamma \vdash \termm : \tyb$}
373     \RightLabel{$\andint$}
374     \BinaryInfC{$\Gamma \vdash (\tya , \tyb) : \tya \wedge \tyb$}
375     \DisplayProof
376     &
377     \AxiomC{$\Gamma \vdash \termt : \tya \wedge \tyb$}
378     \RightLabel{$\andel$}
379     \UnaryInfC{$\Gamma \vdash \lcfst \termt : \tya$}
380     \noLine
381     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb$}
382     \DisplayProof
383   \end{tabular}
384
385   \vspace{0.5cm}
386
387   \begin{tabular}{c c}
388     \AxiomC{$\Gamma \vdash \termt : \bot$}
389     \RightLabel{$\botel$}
390     \UnaryInfC{$\Gamma \vdash \lcabsurdd{\tya} \termt : \tya$}
391     \DisplayProof
392     &
393     \AxiomC{}
394     \RightLabel{$\top I$}
395     \UnaryInfC{$\Gamma \vdash \lcunit : \top$}
396     \DisplayProof
397   \end{tabular}
398 \end{center}
399 \begin{center}
400   \axdesc{reduction}{\termsyn \bred \termsyn}
401   \begin{eqnarray*}
402     \lccase{(\lcinl \termt)}{\termm}{\termn} & \bred & \app{\termm}{\termt} \\
403     \lccase{(\lcinr \termt)}{\termm}{\termn} & \bred & \app{\termn}{\termt} \\
404     \lcfst (\termt , \termm)                 & \bred & \termt \\
405     \lcsnd (\termt , \termm)                 & \bred & \termm
406   \end{eqnarray*}
407 \end{center}
408
409 With these rules, our STLC now looks remarkably similar in power and use to the
410 natural deduction we already know.  $\neg A$ can be expressed as $A \tyarr
411 \bot$.  However, there is an important omission: there is no term of the type $A
412 \vee \neg A$ (excluded middle), or equivalently $\neg \neg A \tyarr A$ (double
413 negation), or indeed any term with a type equivalent to those.
414
415 This has a considerable effect on our logic and it's no coincidence, since there
416 is no obvious computational behaviour for laws like the excluded middle.
417 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
418 and all the systems analysed will have this characteristic since they build on
419 the foundation of the STLC\footnote{There is research to give computational
420   behaviour to classical logic, but we will not touch those subjects.}.
421
422 Finally, going back to our $\mathsf{fix}$ combinator, it's now easy to see how
423 we would want to exclude such a thing if we want to use STLC as a logic, since
424 it allows us to prove everything: $(\lcfix{x : \tya}{x}) : \tya$ clearly works
425 for any $A$!  This is a crucial point: in general we wish to have systems that
426 do not let the user devise a term of type $\bot$, otherwise our logic will be
427 unsound\footnote{Obviously such a term can be present under a $\lambda$.}.
428
429 \subsection{Extending the STLC}
430
431 \newcommand{\lctype}{\mathsf{Type}}
432 \newcommand{\lcite}[3]{\lcsyn{if}\appsp#1\appsp\lcsyn{then}\appsp#2\appsp\lcsyn{else}\appsp#3}
433 \newcommand{\lcbool}{\mathsf{Bool}}
434 \newcommand{\lcforallz}[2]{\forall #1 \absspace.\absspace #2}
435 \newcommand{\lcforall}[3]{(#1 : #2) \tyarr #3}
436 \newcommand{\lcexists}[3]{\exists #1 : #2 \absspace.\absspace #3}
437
438 The STLC can be made more expressive in various ways.  Henk Barendregt
439 succinctly expressed geometrically how we can expand our type system:
440
441 \begin{equation*}
442 \xymatrix@!0@=1.5cm{
443   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
444   & & \lambda C \ar@{-}[dd]
445   \\
446   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
447   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
448   \\
449   & \lambda\underline\omega \ar@{-}'[r][rr]
450   & & \lambda P\underline\omega
451   \\
452   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
453   & & \lambda P \ar@{-}[ur]
454 }
455 \end{equation*}
456 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
457 3 dimensions:
458 \begin{description}
459 \item[Terms depending on types (towards $\lambda{2}$)] In other words, we can
460   quantify over types in our type signatures: $(\abs{A : \lctype}{\abs{x :
461       A}{x}}) : \lcforallz{A}{A \tyarr A}$.  The first and most famous instance
462   of this idea has been System F.  This gives us a form of polymorphism and has
463   been wildly successful, also thanks to a well known inference algorithm for a
464   restricted version of System F known as Hindley-Milner.  Languages like
465   Haskell and SML are based on this discipline.
466 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] In other
467   words, we have type operators: $(\abs{A : \lctype}{\abs{R : \lctype}{(A \to R) \to R}}) : \lctype \to \lctype \to \lctype$.
468 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
469   types', give great expressive power: $(\abs{x : \lcbool}{\lcite{x}{\mathbb{N}}{\mathbb{Q}}}) : \lcbool \to \lctype$.
470 \end{description}
471
472 All the systems preserve the properties that make the STLC well behaved (some of
473 which I haven't mentioned yet).  The system we are going to focus on,
474 Intuitionistic Type Theory, has all of the above additions, and thus would sit
475 where $\lambda{C}$ sits in the `$\lambda$-cube' above.
476
477 \section{Intuitionistic Type Theory and dependent types}
478
479 \newcommand{\lcset}[1]{\mathsf{Type}_{#1}}
480 \newcommand{\lcsetz}{\mathsf{Type}}
481 \newcommand{\defeq}{\equiv}
482
483 \subsection{A Bit of History}
484
485 Logic frameworks and programming languages based on type theory have a long
486 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
487 but then revised it since the original version was too impredicative and thus
488 inconsistent\footnote{In the early version $\lcsetz : \lcsetz$, see section
489   \ref{sec:core-tt} for an explanation on why this causes problems.}.  For this
490 reason he gave a revised and consistent definition later \citep{Martin-Lof1984}.
491
492 A related development is the one of the polymorphic $\lambda$-calculus, and
493 specifically the previously mentioned System F, which was invented independently
494 by Girard and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The
495 surprising fact is that while System F is impredicative it is still consistent
496 and strongly normalising.  \cite{Coquand1986} Huet further extended this line of
497 work with the Calculus of Constructions (CoC).
498
499 \subsection{A Core Type Theory}
500 \label{sec:core-tt}
501
502 The calculus I present follows the exposition in \citep{Thompson1991}, and as
503 said previously is quite close to the original formulation of predicative ITT as
504 found in \citep{Martin-Lof1984}.
505
506 \begin{center}
507   \axname{syntax}
508   \begin{eqnarray*}
509   \termsyn & ::= & x \\
510          &  |  & \lcforall{x}{\termsyn}{\termsyn} \separ \abs{x : \termsyn}{\termsyn} \separ \app{\termsyn}{\termsyn} \\
511          &  |  & \lcexists{x}{\termsyn}{\termsyn} \separ (\termsyn , \termsyn)_{x.\termsyn} \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
512          &  |  & \bot \separ \lcabsurd_{\termsyn} \termsyn \\
513          &  |  & \lcset{n} \\
514    n     & \in & \mathbb{N}
515  \end{eqnarray*}
516
517   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
518
519   \vspace{0.5cm}
520
521   \begin{tabular}{c c c}
522     \AxiomC{}
523     \RightLabel{var}
524     \UnaryInfC{$\Gamma;x : \tya \vdash x : \tya$}
525     \DisplayProof
526     &
527     \AxiomC{$\Gamma \vdash \termt : \bot$}
528     \UnaryInfC{$\Gamma \vdash \lcabsurdd{\tya} \termt : \tya$}
529     \DisplayProof
530     &
531     \AxiomC{$\Gamma \vdash \termt : \tya$}
532     \AxiomC{$\tya \defeq \tyb$}
533     \BinaryInfC{$\Gamma \vdash \termt : \tyb$}
534     \DisplayProof
535   \end{tabular}
536
537   \vspace{0.5cm}
538
539   \begin{tabular}{c c}
540     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
541     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \lcforall{x}{\tya}{\tyb}$}
542     \DisplayProof
543     &
544     \AxiomC{$\Gamma \vdash \termt : \lcforall{x}{\tya}{\tyb}$}
545     \AxiomC{$\Gamma \vdash \termm : \tya$}
546     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb[\termm ]$}
547     \DisplayProof
548   \end{tabular}
549
550   \vspace{0.5cm}
551
552   \begin{tabular}{c c}
553     \AxiomC{$\Gamma \vdash \termt : \tya$}
554     \AxiomC{$\Gamma \vdash \termm : \tyb[\termt ]$}
555     \BinaryInfC{$\Gamma \vdash (\termt, \termm)_{x.\tyb} : \lcexists{x}{\tya}{\tyb}$}
556     \DisplayProof
557     &
558     \AxiomC{$\Gamma \vdash \termt: \lcexists{x}{\tya}{\tyb}$}
559     \UnaryInfC{$\hspace{0.7cm} \Gamma \vdash \lcfst \termt : \tya \hspace{0.7cm}$}
560     \noLine
561     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb[\lcfst \termt ]$}
562     \DisplayProof
563   \end{tabular}
564
565   \vspace{0.5cm}
566
567   \begin{tabular}{c c}
568     \AxiomC{}
569     \UnaryInfC{$\Gamma \vdash \lcset{n} : \lcset{n + 1}$}
570     \DisplayProof
571     &
572     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
573     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
574     \BinaryInfC{$\Gamma \vdash \lcforall{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
575     \noLine
576     \UnaryInfC{$\Gamma \vdash \lcexists{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
577     \DisplayProof
578   \end{tabular}
579
580   \vspace{0.5cm}
581
582   \axdesc{reduction}{\termsyn \bred \termsyn}
583   \begin{eqnarray*}
584     \app{(\abs{x}{\termt})}{\termm} & \bred & \termt[\termm ] \\
585     \lcfst (\termt, \termm) & \bred & \termt \\
586     \lcsnd (\termt, \termm) & \bred & \termm
587   \end{eqnarray*}
588 \end{center}
589
590 There are a lot of new factors at play here. The first thing to notice is that
591 the separation between types and terms is gone.  All we have is terms, that
592 include both values (terms of type $\lcset{0}$) and types (terms of type
593 $\lcset{n}$, with $n > 0$).  This change is reflected in the typing rules.
594 While in the STLC values and types are kept well separated (values never go
595 `right of the colon'), in ITT types can freely depend on values.
596
597 This relation is expressed in the typing rules for $\tyarr$ and $\exists$: if a
598 function has type $\lcforall{x}{\tya}{\tyb}$, $\tyb$ can depend on $x$.
599 Examples will make this clearer once some base types are added in section
600 \ref{sec:base-types}.
601
602 $\tyarr$ and $\exists$ are at the core of the machinery of ITT:
603
604 \begin{description}
605 \item[`forall' ($\tyarr$)] is a generalisation of $\tyarr$ in the STLC and
606   expresses universal quantification in our logic.  It is often written with a
607   syntax closer to logic, e.g. $\forall x : \mathsf{Bool}. \dotsb$.  In the
608   literature this is also known as `dependent product' and shown as $\Pi$,
609   following the interpretation of functions as infinitary products. We will just
610   call it `dependent function', reserving `product' for $\exists$.
611
612 \item[`exists' ($\exists$)] is a generalisation of $\wedge$ in the extended
613   STLC of section \ref{sec:curry-howard}, and thus we will call it `dependent
614   product'.  Like $\wedge$, it is formed by providing a pair of things.  In our
615   logic, it represents existential quantification.
616
617   For added confusion, in the literature that calls forall $\Pi$, $\exists$ is
618   often named `dependent sum' and shown as $\Sigma$.  This is following the
619   interpretation of $\exists$ as a generalised, infinitary $\vee$, where the
620   first element of the pair is the `tag' that decides which type the second
621   element will have.
622 \end{description}
623
624 Another thing to notice is that types are very `first class': we are free to
625 create functions that accept and return types.  For this reason we $\defeq$ as
626 the smallest equivalence relation extending $\bredc$, where $\bredc$ is the
627 reflexive transitive closure of $\bred$; and we treat types that are equal
628 according to $\defeq$ as the same.  Another way of seeing $\defeq$ is this: when
629 we want to compare two types for equality, we reduce them as far as possible and
630 then check if they are equal\footnote{Note that when comparing terms we do it up
631   to $\alpha$-renaming.  That is, we do not consider relabelling of variables as
632   a difference - for example $\abs{x : A}{x} \defeq \abs{y : A}{y}$.}.  This
633 works since not only each term has a normal form (ITT is strongly normalising),
634 but the normal form is also unique; or in other words $\bred$ is confluent (if
635 $\termt \bredc \termm$ and $\termt \bredc \termn$, then $\termm \bredc \termp$
636 and $\termn \bredc \termp$).  This measure makes sure that, for instance,
637 $\app{(\abs{x : \lctype}{x})}{\lcbool} \defeq \lcbool$.  The theme of equality
638 is central and will be analysed better later.
639
640 The theory presented is \emph{stratified}.  We have a hierarchy of types
641 $\lcset{0} : \lcset{1} : \lcset{2} : \dots$, so that there is no `type of all
642 types', and our theory is predicative.  The layers of the hierarchy are called
643 `universes'.  $\lcsetz : \lcsetz$ ITT is inconsistent due to Girard's paradox
644 \citep{Hurkens1995}, and thus loses its well-behavedness.  Some impredicativity
645 sometimes has its place, either because the theory retain good properties
646 (normalization, consistency, etc.) anyway, like in System F and CoC; or because
647 we are at a stage at which we do not care - we will see instances of the last
648 motivation later.  Moreover, universes can be inferred mechanically
649 \citep{Pollack1990}. It is also convenient to have a \emph{cumulative} theory,
650 where $\lcset{n} : \lcset{m}$ iff $n < m$.  We eschew these measures to keep the
651 presentation simple.
652
653 Lastly, the theory I present is fully explicit in the sense that the user has to
654 specify every type when forming abstractions, products, etc.  This can be a
655 great burden if one wants to use the theory directly.  Complete inference is
656 undecidable (which is hardly surprising considering the role that types play)
657 but partial inference (also called `bidirectional type checking' in this
658 context) in the style of \cite{Pierce2000} will have to be deployed in a
659 practical system.  When showing examples types will be omitted when this can be
660 done without loss of clarity, for example $\abs{x}{x}$ in place of $\abs{A :
661   \lcsetz}{\abs{x : A}{x}}$.
662
663 Note that the Curry-Howard correspondence runs through ITT as it did with the
664 STLC with the difference that ITT corresponds to an higher order propositional
665 logic.
666
667 % TODO describe abbreviations somewhere
668 % I will use various abbreviations:
669 % \begin{itemize}
670 %   \item $\lcsetz$ for $\lcset{0}$
671 %   \item $\tya \tyarr \tyb$ for $\lcforall{-}{\tya}{\tyb}$, when $\tyb$ does not
672 %     depend on the value of type $\tya$
673 %   \item $(
674
675 \subsection{Base Types}
676 \label{sec:base-types}
677
678 \newcommand{\lctrue}{\mathsf{true}}
679 \newcommand{\lcfalse}{\mathsf{false}}
680 \newcommand{\lcw}[3]{\mathsf{W} #1 : #2 \absspace.\absspace #3}
681 \newcommand{\lcnode}[4]{#1 \lhd_{#2 . #3} #4}
682 \newcommand{\lcnodez}[2]{#1 \lhd #2}
683 \newcommand{\lcited}[5]{\lcsyn{if}\appsp#1/#2\appsp.\appsp#3\appsp\lcsyn{then}\appsp#4\appsp\lcsyn{else}\appsp#5}
684 \newcommand{\lcrec}[4]{\lcsyn{rec}\appsp#1/#2\appsp.\appsp#3\appsp\lcsyn{with}\appsp#4}
685 \newcommand{\lcrecz}[2]{\lcsyn{rec}\appsp#1\appsp\lcsyn{with}\appsp#2}
686 \newcommand{\AxiomL}[1]{\Axiom$\fCenter #1$}
687 \newcommand{\UnaryInfL}[1]{\UnaryInf$\fCenter #1$}
688
689 While the ITT presented is a fairly complete logic, it is not that useful for
690 programming.  If we wish to make it better, we can add some base types to
691 represent the data structures we know and love, such as numbers, lists, and
692 trees.  Apart from some unsurprising data types, we introduce $\mathsf{W}$, a
693 very general tree-like structure useful to represent inductively defined types.
694
695 \begin{center}
696   \axname{syntax}
697   \begin{eqnarray*}
698   \termsyn & ::= & \dots \\
699            &  |  & \top \separ \lcunit \\
700            &  |  & \lcbool \separ \lctrue \separ \lcfalse \separ \lcited{\termsyn}{x}{\termsyn}{\termsyn}{\termsyn} \\
701            &  |  & \lcw{x}{\termsyn}{\termsyn} \separ \lcnode{\termsyn}{x}{\termsyn}{\termsyn} \separ \lcrec{\termsyn}{x}{\termsyn}{\termsyn}
702  \end{eqnarray*}
703
704   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
705
706   \vspace{0.5cm}
707
708   \begin{tabular}{c c c}
709     \AxiomC{}
710     \UnaryInfC{$\hspace{0.2cm}\Gamma \vdash \top : \lcset{0} \hspace{0.2cm}$}
711     \noLine
712     \UnaryInfC{$\Gamma \vdash \lcbool : \lcset{0}$}
713     \DisplayProof
714     &
715     \AxiomC{}
716     \UnaryInfC{$\Gamma \vdash \lcunit : \top$}
717     \DisplayProof
718     &
719     \AxiomC{}
720     \UnaryInfC{$\Gamma \vdash \lctrue : \lcbool$}
721     \noLine
722     \UnaryInfC{$\Gamma \vdash \lcfalse : \lcbool$}
723     \DisplayProof
724   \end{tabular}
725
726   \vspace{0.5cm}
727
728   \begin{tabular}{c}
729     \AxiomC{$\Gamma \vdash \termt : \lcbool$}
730     \AxiomC{$\Gamma \vdash \termm : \tya[\lctrue]$}
731     \AxiomC{$\Gamma \vdash \termn : \tya[\lcfalse]$}
732     \TrinaryInfC{$\Gamma \vdash \lcited{\termt}{x}{\tya}{\termm}{\termn} : \tya[\termt]$}
733     \DisplayProof
734   \end{tabular}
735
736   \vspace{0.5cm}
737
738   \begin{tabular}{c}
739     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
740     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
741     \BinaryInfC{$\Gamma \vdash \lcw{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
742     \DisplayProof
743   \end{tabular}
744
745   \vspace{0.5cm}
746
747   \begin{tabular}{c}
748     \AxiomC{$\Gamma \vdash \termt : \tya$}
749     \AxiomC{$\Gamma \vdash \termf : \tyb[\termt ] \tyarr \lcw{x}{\tya}{\tyb}$}
750     \BinaryInfC{$\Gamma \vdash \lcnode{\termt}{x}{\tyb}{\termf} : \lcw{x}{\tya}{\tyb}$}
751     \DisplayProof
752   \end{tabular}
753
754   \vspace{0.5cm}
755
756   \begin{tabular}{c}
757     \AxiomC{$\Gamma \vdash \termt: \lcw{x}{\tya}{\tyb}$}
758     \noLine
759     \UnaryInfC{$\Gamma \vdash \lcforall{\termm}{\tya}{\lcforall{\termf}{(\tyb[\termm] \tyarr \lcw{x}{\tya}{\tyb})}{(\lcforall{\termn}{\tyb[\termm]}{\tyc[\app{\termf}{\termn}]}) \tyarr \tyc[\lcnodez{\termm}{\termf}]}}$}
760     \UnaryInfC{$\Gamma \vdash \lcrec{\termt}{x}{\tyc}{\termp} : \tyc[\termt]$}
761     \DisplayProof
762   \end{tabular}
763
764   \vspace{0.5cm}
765
766   \axdesc{reduction}{\termsyn \bred \termsyn}
767   \begin{eqnarray*}
768     \lcited{\lctrue}{x}{\tya}{\termt}{\termm} & \bred & \termt \\
769     \lcited{\lcfalse}{x}{\tya}{\termt}{\termm} & \bred & \termm \\
770     \lcrec{\lcnodez{\termt}{\termf}}{x}{\tya}{\termp} & \bred & \app{\app{\app{\termp}{\termt}}{\termf}}{(\abs{\termm}{\lcrec{\app{f}{\termm}}{x}{\tya}{\termp}})}
771   \end{eqnarray*}
772 \end{center}
773
774 The introduction and elimination for $\top$ and $\lcbool$ are unsurprising.
775 Note that in the $\lcite{\dotsb}{\dotsb}{\dotsb}$ construct the type of the
776 branches are dependent on the value of the conditional.
777
778 The rules for $\mathsf{W}$, on the other hand, are quite an eyesore.  The idea
779 behind $\mathsf{W}$ types is to build up `trees' where shape of the number of
780 `children' of each node is dependent on the value in the node.  This is
781 captured by the $\lhd$ constructor, where the argument on the left is the value,
782 and the argument on the right is a function that returns a child for each
783 possible value of $\tyb[\text{node value}]$, if $\lcw{x}{\tya}{\tyb}$.  The
784 recursor $\lcrec{\termt}{x}{\tyc}{\termp}$ uses $p$ to inductively prove that
785 $\tyc[\termt]$ holds.
786
787 \subsection{Some examples}
788
789 Now we can finally provide some meaningful examples.  Apart from omitting types,
790 I will also use some abbreviations:
791 \begin{itemize}
792   \item $\_\mathit{operator}\_$ to define infix operators
793   \item $\abs{x\appsp y\appsp z : \tya}{\dotsb}$ to define multiple abstractions at the same
794     time
795 \end{itemize}
796
797 \subsubsection{Sum types}
798
799 We would like to recover our sum type, or disjunction, $\vee$.  This is easily
800 done with $\exists$:
801 \[
802 \begin{array}{rcl}
803   \_\vee\_ & = &  \abs{\tya\appsp\tyb : \lcsetz}{\lcexists{x}{\lcbool}{\lcite{x}{\tya}{\tyb}}} \\
804   \lcinl   & = & \abs{x}{(\lctrue, x)} \\
805   \lcinr   & = & \abs{x}{(\lcfalse, x)} \\
806   \mathsf{case} & = & \abs{x : \tya \vee \tyb}{\abs{f : \tya \tyarr \tyc}{\abs{g : \tyb \tyarr \tyc}{ \\
807            &   & \hspace{0.5cm} \app{(\lcited{\lcfst x}{b}{(\lcite{b}{A}{B}) \tyarr C}{f}{g})}{(\lcsnd x)}}}}
808 \end{array}
809 \]
810 What is going on here?  We are using $\exists$ with $\lcbool$ as a tag, so that
811 we can choose between one of two types in the second element.  In
812 $\mathsf{case}$ we use $\lcite{\lcfst x}{\dotsb}{\dotsb}$ to discriminate on the
813 tag, that is, the first element of $x : \tya \vee \tyb$.  If the tag is true,
814 then we know that the second element is of type $\tya$, and we will apply $f$.
815 The same applies to the other branch, with $\tyb$ and $g$.
816
817 \subsubsection{Naturals}
818
819 Now it's time to showcase the power of $\mathsf{W}$ types.
820 \begin{eqnarray*}
821   \mathsf{Nat}  & = & \lcw{b}{\lcbool}{\lcite{b}{\top}{\bot}}  \\
822   \mathsf{zero} & = & \lcfalse \lhd \abs{z}{\lcabsurd z} \\
823   \mathsf{suc}  & = & \abs{n}{(\lctrue \lhd \abs{\_}{n})}  \\
824   \mathsf{plus} & = & \abs{x\appsp y}{\lcrecz{x}{(\abs{b}{\lcite{b}{\abs{\_\appsp f}{\app{\mathsf{suc}}{(\app{f}{\lcunit})}}}{\abs{\_\appsp\_}{y}}})}}
825 \end{eqnarray*}
826 Here we encode natural numbers through $\mathsf{W}$ types.  Each node contains a
827 $\lcbool$.  If the $\lcbool$ is $\lcfalse$, then there are no children, since we
828 have a function $\bot \tyarr \mathsf{Nat}$: this is our $0$.  If it's $\lctrue$,
829 we need one child only: the predecessor.  Similarly, we recurse giving to
830 $\lcsyn{rec}$ a function that handles the `base case' 0 when the $\lcbool$ is
831 $\lctrue$, and the inductive case otherwise.
832
833 We postpone more complex examples after the introduction of inductive families
834 and pattern matching, since $\mathsf{W}$ types get unwieldy very quickly.
835
836 \subsubsection{Propositional Equality}
837
838 We can finally introduce one of the central subjects of my project:
839 propositional equality.
840
841 \newcommand{\lceq}[3]{#2 =_{#1} #3}
842 \newcommand{\lceqz}[2]{#1 = #2}
843 \newcommand{\lcrefl}[2]{\mathsf{refl}_{#1}\appsp #2}
844 \newcommand{\lcsubst}[3]{\mathsf{subst}\appsp#1\appsp#2\appsp#3}
845
846 \begin{center}
847   \axname{syntax}
848   \begin{eqnarray*}
849   \termsyn & ::= & ... \\
850            &  |  & \lceq{\termsyn}{\termsyn}{\termsyn} \separ \lcrefl{\termsyn}{\termsyn} \separ \lcsubst{\termsyn}{\termsyn}{\termsyn}
851  \end{eqnarray*}
852
853   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
854
855   \vspace{0.5cm}
856
857   \begin{tabular}{c c}
858     \AxiomC{$\Gamma \vdash \termt : \tya$}
859     \UnaryInfC{$\Gamma \vdash \lcrefl{\tya}{\termt} : \lceq{\tya}{\termt}{\termt}$}
860     \DisplayProof
861     &
862     \AxiomC{$\Gamma \vdash \termp : \lceq{\tya}{\termt}{\termm}$}
863     \AxiomC{$\Gamma \vdash \tyb : \tya \tyarr \lcsetz$}
864     \AxiomC{$\Gamma \vdash \termn : \app{\tyb}{\termt}$}
865     \TrinaryInfC{$\Gamma \vdash \lcsubst{\termp}{\tyb}{\termn} : \app{\tyb}{\termm}$}
866     \DisplayProof
867   \end{tabular}
868
869   \vspace{0.5cm}
870
871   \axdesc{reduction}{\termsyn \bred \termsyn}
872   \begin{eqnarray*}
873     \lcsubst{(\lcrefl{\tya}{\termt})}{\tyb}{\termm} \bred \termm
874   \end{eqnarray*}
875 \end{center}
876
877 Propositional equality internalises equality as a type.  This lets us prove
878 useful things.  The only way to introduce an equality proof is by reflexivity
879 ($\mathsf{refl}$).  The eliminator is in the style of `Leibnitz's law' of
880 equality: `equals can be substituted for equals' ($\mathsf{subst}$).  The
881 computation rule refers to the fact that every canonical proof of equality will
882 be a $\mathsf{refl}$.  We can use $\neg (\lceq{\tya}{x}{y})$ to express
883 inequality.
884
885 These elements conclude our presentation of a `core' type theory.  For an
886 extended example see Section 6.2 of \cite{Thompson1991}\footnote{Note that while I
887   attempted to formalise the proof in Agda, I found a bug in the book!  See the
888   errata for details:
889   \url{http://www.cs.kent.ac.uk/people/staff/sjt/TTFP/errata.html}.}.  The above
890 language and examples have been codified in Agda\footnote{More on Agda in
891   the next section.}, see appendix \ref{app:agda-code}.
892
893 \section{A more useful language}
894
895 While our core type theory equipped with $\mathsf{W}$ types is very usefully
896 conceptually as a simple but complete language, things get messy very fast.  In
897 this section we will present the elements that are usually include in theorem
898 provers or programming languages to make them usable by mathematicians or
899 programmers.
900
901 All the features presented, except for quotient types, are present in the second
902 version of the Agda system, which is described in the PhD thesis of the author
903 \citep{Norell2007}.  Agda follows a tradition of theorem provers based on ITT
904 with inductive families and pattern matching.  The closest relative of Agda,
905 apart from previous versions, is Epigram, whose type theory is described in
906 \cite{McBride2004}.  Coq is another notable theorem prover based on the same
907 concepts, and the first and most popular \cite{Coq}.
908
909 The main difference between Coq and Agda/Epigram is that the former focuses on
910 theorem proving, while the latter also want to be useful as functional
911 programming languages, while still offering good tools to express
912 mathematics\footnote{In fact, currently, Agda is used almost exclusively to
913   express mathematics, rather than to program.}.  This is reflected in a series
914 of differences that I will not discuss here (most notably the absence of tactics
915 but better support for pattern matching in Agda/Epigram).  I will take the same
916 approach as Agda/Epigram and will give a perspective focused on functional
917 programming rather than theorem proving.  Every feature will be presented as it
918 is present in Agda (if possible, since some features are not present in Agda
919 yet).
920
921 \subsection{Inductive families}
922
923 \newcommand{\lcdata}[1]{\lcsyn{data}\appsp #1}
924 \newcommand{\lcdb}{\ |\ }
925 \newcommand{\lcind}{\hspace{0.2cm}}
926 \newcommand{\lcwhere}{\appsp \lcsyn{where}}
927 \newcommand{\lclist}[1]{\app{\mathsf{List}}{#1}}
928 \newcommand{\lcnat}{\app{\mathsf{Nat}}}
929 \newcommand{\lcvec}[2]{\app{\app{\mathsf{Vec}}{#1}}{#2}}
930
931 Inductive families were first introduced by \cite{Dybjer1991}.  For the reader
932 familiar with the recent developments present in the GHC compiler for Haskell,
933 inductive families will look similar to GADTs (Generalised Abstract Data Types).
934 % TODO: add citation
935 Haskell style data types provide \emph{parametric polymorphism}, so that we can
936 define types that range over type parameters:
937 \[
938 \begin{array}{l}
939 \lcdata{\lclist{A}} = \mathsf{Nil} \lcdb A :: \lclist{A}
940 \end{array}
941 \]
942 In this way we define the $\mathsf{List}$ type once while allowing elements to
943 be of any type.  $\mathsf{List}$ will be a type constructor of type $\lcsetz
944 \tyarr \lcsetz$, while $\mathsf{nil} : \lcforall{A}{\lcsetz}{\lclist{A}}$ and
945 $\_::\_ : \lcforall{A}{\lcsetz}{A \tyarr \lclist{A} \tyarr \lclist{A}}$.
946
947 Inductive families bring this concept one step further by allowing some of the
948 parameters to be constrained in constructors.  We call these parameters
949 `indices'.  For example we might define natural numbers
950 \[
951 \begin{array}{l}
952 \lcdata{\lcnat} : \lcsetz \lcwhere \\
953   \begin{array}{l@{\ }l}
954     \lcind \mathsf{zero} &: \lcnat \\
955     \lcind \mathsf{suc}  &: \lcnat \tyarr \lcnat
956   \end{array}
957 \end{array}
958 \]
959 And then define a type for lists indexed by length:
960 \[
961 \begin{array}{l}
962 \lcdata{\mathsf{Vec}}\appsp (A : \lcsetz) : \lcnat \tyarr \lcsetz \lcwhere \\
963   \begin{array}{l@{\ }l}
964     \lcind \mathsf{nil} &: \lcvec{A}{\mathsf{zero}} \\
965     \lcind \_::\_ &: \{n : \mathsf{Nat}\} \tyarr A \tyarr \lcvec{A}{n} \tyarr \lcvec{A}{\app{(\mathsf{suc}}{n})}
966   \end{array}
967 \end{array}
968 \]
969 Note that contrary to the Haskell ADT notation, with inductive families we
970 explicitly list the types for the type constructor and data constructors.  In
971 $\mathsf{Vec}$, $A$ is a parameter (same across all constructors) while the
972 $\lcnat$ is an integer.  In our syntax, in the $\lcsyn{data}$ declaration,
973 things to the left of the colon are parameters, while on the right we can define
974 the type of indices.  Also note that the parameters can be named across all
975 constructors, while indices can't.
976
977 In this $\mathsf{Vec}$ example, when we form a new list the length is
978 $\mathsf{zero}$.  When we append a new element to an existing list of length
979 $n$, the new list is of length $\app{\mathsf{suc}}{n}$, that is, one more than
980 the previous length.  Also note that we are using the $\{n : \lcnat\} \tyarr
981 \dotsb$ syntax to indicate an argument that we will omit when using $\_::\_$.
982 In the future I shall also omit the type of these implicit parameters, in line
983 how Agda works.
984
985 Once we have our $\mathsf{Vec}$ we can do things much more safely than with
986 normal lists.  For example, we can define an $\mathsf{head}$ function that
987 returns the first element of the list:
988 \[
989 \begin{array}{l}
990 \mathsf{head} : \{A\ n\} \tyarr \lcvec{A}{(\app{\mathsf{suc}}{n})} \tyarr A
991 \end{array}
992 \]
993 Note that we will not be able to provide this function with a
994 $\lcvec{A}{\mathsf{zero}}$, since it needs an index which is a successor of some
995 number.
996
997 \newcommand{\lcfin}[1]{\app{\mathsf{Fin}}{#1}}
998
999 If we wish to index a $\mathsf{Vec}$ safely, we can define an inductive family
1000 $\lcfin{n}$, which represents the family of numbers smaller than a given number
1001 $n$:
1002 \[
1003 \begin{array}{l}
1004   \lcdata{\mathsf{Fin}} : \lcnat \tyarr \lcsetz \lcwhere \\
1005   \begin{array}{l@{\ }l}
1006     \lcind \mathsf{fzero} &: \{n\} \tyarr \lcfin{n} \\
1007     \lcind \mathsf{fsuc}  &: \{n\} \tyarr (i : \lcfin{n}) \tyarr \lcfin{(\app{\mathsf{suc}}{n})}
1008   \end{array}
1009 \end{array}
1010 \]
1011 $\mathsf{fzero}$ is smaller than any number apart from $\mathsf{zero}$.  Given
1012 the family of numbers smaller than $i$, we can produce the family of numbers
1013 smaller than $\app{\mathsf{suc}}{n}$.  Now we can define an `indexing' operation
1014 for our $\mathsf{Vec}$:
1015 \[
1016 \begin{array}{l}
1017 \_\mathsf{!!}\_ : \{A\ n\} \tyarr \lcvec{A}{n} \tyarr \lcfin{n} \tyarr A
1018 \end{array}
1019 \]
1020 In this way we will be sure that the number provided as an index will be smaller
1021 than the length of the $\mathsf{Vec}$ provided.
1022
1023
1024 \subsection{Pattern matching}
1025
1026 \subsection{Guarded recursion}
1027
1028 \subsection{Corecursion}
1029
1030 \subsection{Quotient types}
1031
1032
1033 \section{Why dependent types?}
1034
1035 \section{Many equalities}
1036
1037 \subsection{Revision}
1038
1039 \subsection{Extensional equality and its problems}
1040
1041 \subsection{Observational equality}
1042
1043 \section{What to do}
1044
1045
1046 \bibliographystyle{authordate1}
1047 \bibliography{background}
1048
1049 \appendix
1050 \section{Agda code}
1051 \label{app:agda-code}
1052
1053 \end{document}