85eedd1c55b71488e213d03a72b22e2532c9999b
[bitonic-mengthesis.git] / docs / InterimReport.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 \usepackage{stmaryrd}
39
40 %% -----------------------------------------------------------------------------
41 %% Utils
42 \usepackage{bussproofs}
43 \usepackage{umoline}
44 \usepackage[export]{adjustbox}
45 \usepackage{multicol}
46 \usepackage{listingsutf8}
47 \lstset{
48   basicstyle=\small\ttfamily,
49   extendedchars=\true,
50   inputencoding=utf8x,
51   xleftmargin=1cm
52 }
53 \usepackage{epigraph}
54
55 %% -----------------------------------------------------------------------------
56 %% Bibtex
57 \usepackage{natbib}
58
59 %% Numbering depth
60 %% \setcounter{secnumdepth}{0}
61
62 %% -----------------------------------------------------------------------------
63 % We will generate all images so they have a width \maxwidth. This means
64 % that they will get their normal width if they fit onto the page, but
65 % are scaled down if they would overflow the margins.
66 \makeatletter
67 \def\maxwidth{
68   \ifdim\Gin@nat@width>\linewidth\linewidth
69   \else\Gin@nat@width\fi
70 }
71 \makeatother
72
73 %% -----------------------------------------------------------------------------
74 %% Links
75 \usepackage[
76   setpagesize=false, % page size defined by xetex
77   unicode=false, % unicode breaks when used with xetex
78   xetex
79 ]{hyperref}
80
81 \hypersetup{
82   breaklinks=true,
83   bookmarks=true,
84   pdfauthor={Francesco Mazzoli <fm2209@ic.ac.uk>},
85   pdftitle={Observational Equality - Interim Report},
86   colorlinks=false,
87   pdfborder={0 0 0}
88 }
89
90 % Make links footnotes instead of hotlinks:
91 % \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
92
93 % avoid problems with \sout in headers with hyperref:
94 \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
95
96 \title{Observational Equality - Interim Report}
97 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
98 \date{December 2012}
99
100 \begin{document}
101
102 \maketitle
103
104 \setlength{\tabcolsep}{12pt}
105
106 \begin{abstract}
107 The marriage between programming and logic has been a very fertile one.  In
108 particular, since the simply typed lambda calculus (STLC), a number of type
109 systems have been devised with increasing expressive power.
110
111 Section \ref{sec:types} I will give a very brief overview of STLC, and then
112 illustrate how it can be interpreted as a natural deduction system.  Section
113 \ref{sec:itt} will introduce Inutitionistic Type Theory (ITT), which expands on
114 this concept, employing a more expressive logic.  The exposition is quite dense
115 since there is a lot of material to cover; for a more complete treatment of the
116 material the reader can refer to \citep{Thompson1991, Pierce2002}. Section
117 \ref{sec:practical} will describe additions common in practical programming
118 languages based on ITT.
119
120 Finally, I will explain why equality has been a tricky business in these
121 theories, and talk about the various attempts have been made to make the
122 situation better.  One interesting development has recently emerged:
123 Observational Type theory.  I propose to explore the ways to turn these ideas
124 into useful practices for programming and theorem proving.
125 \end{abstract}
126
127 \tableofcontents
128
129 \section{Simple and not-so-simple types}
130 \label{sec:types}
131
132 \subsection{A note on notation}
133
134 \newcommand{\appsp}{\hspace{0.07cm}}
135 \newcommand{\app}[2]{#1\appsp#2}
136 \newcommand{\absspace}{\hspace{0.03cm}}
137 \newcommand{\abs}[2]{\lambda #1\absspace.\absspace#2}
138 \newcommand{\termt}{t}
139 \newcommand{\termm}{m}
140 \newcommand{\termn}{n}
141 \newcommand{\termp}{p}
142 \newcommand{\termf}{f}
143 \newcommand{\separ}{\ \ |\ \ }
144 \newcommand{\termsyn}{\mathit{term}}
145 \newcommand{\axname}[1]{\textbf{#1}}
146 \newcommand{\axdesc}[2]{\axname{#1} \fbox{$#2$}}
147 \newcommand{\lcsyn}[1]{\mathrm{\underline{#1}}}
148 \newcommand{\lccon}[1]{\mathsf{#1}}
149 \newcommand{\lcfun}[1]{\mathbf{#1}}
150 \newcommand{\tyarr}{\to}
151 \newcommand{\tysyn}{\mathit{type}}
152 \newcommand{\ctxsyn}{\mathit{context}}
153 \newcommand{\emptyctx}{\cdot}
154
155 In the following sections I will introduce a lot of syntax, typing, and
156 reduction rules, along with examples.  To make the exposition clearer, usually
157 the rules are preceded by what the rule looks like and what it shows (for
158 example \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}).
159
160 In the languages presented I will also use different fonts for different things:
161 \begin{center}
162   \begin{tabular}{c | l}
163     $\lccon{Sans}$  & Sans serif, capitalised, for type constructors. \\
164     $\lccon{sans}$  & Sans serif, not capitalised, for data constructors. \\
165     $\lcsyn{roman}$ & Roman, underlined, for the syntax of the language. \\
166     $\lcfun{roman}$ & Roman, bold, for defined functions and values. \\
167     $math$          & Math mode font for quantified variables and syntax elements.
168   \end{tabular}
169 \end{center}
170
171 Moreover, I will from time to time give examples in the Haskell programming
172 language as defined in \citep{Haskell2010}, which I will typeset in
173 \texttt{typewriter} font.  I assume that the reader is already familiar with
174 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
175
176 \subsection{Untyped $\lambda$-calculus}
177
178 Along with Turing's machines, the earliest attempts to formalise computation
179 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
180 language encodes computation with a minimal syntax and no `data' in the
181 traditional sense, but just functions.
182
183 The syntax of $\lambda$-terms consists of just three things: variables,
184 abstractions, and applications:
185
186 \begin{center}
187 \axname{syntax}
188 $$
189 \begin{array}{rcl}
190   \termsyn & ::= & x \separ (\abs{x}{\termsyn}) \separ (\app{\termsyn}{\termsyn}) \\
191          x & \in & \text{Some enumerable set of symbols, e.g.}\ \{x, y, z, \dots , x_1, x_2, \dots\}
192 \end{array}
193 $$
194 \end{center}
195
196
197 % I will omit parethesis in the usual manner. %TODO explain how
198
199 I will use $\termt,\termm,\termn,\dots$ to indicate a generic term, and $x,y$
200 for variables.  I will also assume that all variable names in a term are unique
201 to avoid problems with name capturing.  Intuitively, abstractions
202 ($\abs{x}{\termt}$) introduce functions with a named parameter ($x$), and
203 applications ($\app{\termt}{\termm}$) apply a function ($\termt$) to an argument
204 ($\termm$).
205
206 The `applying' is more formally explained with a reduction rule:
207
208 \newcommand{\bred}{\leadsto}
209 \newcommand{\bredc}{\bred^*}
210
211 \begin{center}
212 \axdesc{reduction}{\termsyn \bred \termsyn}
213 $$\app{(\abs{x}{\termt})}{\termm} \bred \termt[\termm / x]$$
214 \end{center}
215
216 Where $\termt[\termm / x]$ expresses the operation that substitutes all
217 occurrences of $x$ with $\termm$ in $\termt$.  In the future, I will use
218 $[\termt]$ as an abbreviation for $[\termt / x]$.  In the systems presented, the
219 $\bred$ relation also includes reduction of subterms, for example if $\termt
220 \bred \termm$ then $\app{\termt}{\termn} \bred \app{\termm}{\termn}$, and so on.
221
222 These few elements are of remarkable expressiveness, and in fact Turing
223 complete.  As a corollary, we must be able to devise a term that reduces forever
224 (`loops' in imperative terms):
225 \[
226   \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
227 \]
228 Terms that can be reduced only a finite number of times (the non-looping ones)
229 are said to be \emph{normalising}, and the `final' term (where no reductions are
230 possible on the term or on its subterms) is called \emph{normal form}.
231
232 \subsection{The simply typed $\lambda$-calculus}
233 \label{sec:stlc}
234
235 \newcommand{\tya}{A}
236 \newcommand{\tyb}{B}
237 \newcommand{\tyc}{C}
238
239 One way to `discipline' $\lambda$-terms is to assign \emph{types} to them, and
240 then check that the terms that we are forming make sense given our typing rules
241 \citep{Curry1934}.
242
243 We wish to introduce rules of the form $\Gamma \vdash \termt : \tya$, which
244 reads `in context $\Gamma$, term $\termt$ has type $\tya$'.
245
246 The syntax for types is as follows:
247
248 \begin{center}
249   \axname{syntax}
250    $$\tysyn ::= x \separ \tysyn \tyarr \tysyn$$
251 \end{center}
252
253 I will use $\tya,\tyb,\dots$ to indicate a generic type.
254
255 A context $\Gamma$ is a map from variables to types.  I will use the notation
256 $\Gamma; x : \tya$ to augment it and to `extract' pairs from it.
257
258 Predictably, $\tya \tyarr \tyb$ is the type of a function from $\tya$ to
259 $\tyb$.  We need to be able to decorate our abstractions with
260 types\footnote{Actually, we don't need to: computers can infer the right type
261   easily, but that is another story.}:
262 \begin{center}
263   \axname{syntax}
264    $$\termsyn ::= x \separ (\abs{x : \tysyn}{\termsyn}) \separ (\app{\termsyn}{\termsyn})$$
265 \end{center}
266 Now we are ready to give the typing judgements:
267
268 \begin{center}
269   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
270
271   \vspace{0.5cm}
272
273   \begin{tabular}{c c c}
274     \AxiomC{\phantom{1em}}
275     \UnaryInfC{$\Gamma; x : \tya \vdash x : \tya$}
276     \DisplayProof
277     &
278     \AxiomC{$\Gamma; x : \tya \vdash \termt : \tyb$}
279     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \tya \tyarr \tyb$}
280     \DisplayProof
281   \end{tabular}
282
283   \vspace{0.5cm}
284
285   \begin{tabular}{c}
286     \AxiomC{$\Gamma \vdash \termt : \tya \tyarr \tyb$}
287     \AxiomC{$\Gamma \vdash \termm : \tya$}
288     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb$}
289     \DisplayProof
290   \end{tabular}
291 \end{center}
292
293 This typing system takes the name of `simply typed lambda calculus' (STLC),
294 and enjoys a number of properties.  Two of them are expected in most type
295 systems \citep{Pierce2002}:
296 \begin{description}
297 \item[Progress] A well-typed term is not stuck - either it is a \emph{canonical}
298   value or it can take a step according to the evaluation rules.  With canonical
299   value we indicate terms formed by canonical constructors, in this case only
300   $\lambda$\footnote{See section \ref{sec:fun-ext} for more information on
301     canonicity.}.
302 \item[Preservation] If a well-typed term takes a step of evaluation, then the
303   resulting term is also well typed.
304 \end{description}
305
306 However, STLC buys us much more: every well-typed term
307 is normalising.  It is easy to see that we can't fill the blanks if we want to
308 give types to the non-normalising term shown before:
309 \begin{equation*}
310   \app{(\abs{x : ?}{\app{x}{x}})}{(\abs{x : ?}{\app{x}{x}})}
311 \end{equation*}
312
313 \newcommand{\lcfix}[2]{\lcsyn{fix} \appsp #1\absspace.\absspace #2}
314
315 This makes the STLC Turing incomplete.  We can recover the ability to loop by
316 adding a combinator that recurses:
317 $$
318   \termsyn ::= \dotsb \separ  \lcfix{x : \tysyn}{\termsyn}
319 $$
320 \begin{center}
321   \begin{prooftree}
322     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
323     \UnaryInfC{$\Gamma \vdash \lcfix{x : \tya}{\termt} : \tya$}
324   \end{prooftree}
325 \end{center}
326 $$
327   \lcfix{x : \tya}{\termt} \bred \termt[\lcfix{x : \tya}{\termt}]
328 $$
329 Which will deprive us of normalisation, which is a particularly bad thing if we
330 want to use the STLC as described in the next section.
331
332 \subsection{The Curry-Howard correspondence}
333 \label{sec:curry-howard}
334
335 \newcommand{\lcunit}{\lcfun{\langle\rangle}}
336
337 It turns out that the STLC can be seen a natural deduction system for
338 propositional logic.  Terms are proofs, and their types are the propositions
339 they prove.  This remarkable fact is known as the Curry-Howard correspondence,
340 or isomorphism.
341
342 The `arrow' ($\to$) type corresponds to implication.  If we wish to prove that
343 $(\tya \tyarr \tyb) \tyarr (\tyb \tyarr \tyc) \tyarr (\tya \tyarr \tyc)$, all we
344 need to do is to devise a $\lambda$-term that has the correct type:
345 \begin{equation*}
346   \abs{f : (\tya \tyarr \tyb)}{\abs{g : (\tyb \tyarr \tyc)}{\abs{x : \tya}{\app{g}{(\app{f}{x})}}}}
347 \end{equation*}
348 That is, function composition.  We might want extend our bare lambda calculus
349 with a couple of terms former to make our natural deduction more pleasant to
350 use.  For example, tagged unions (\texttt{Either} in Haskell) are disjunctions,
351 and tuples (or products) are conjunctions.  We also want to be able to express
352 falsity ($\bot$): that can done by introducing a type inhabited by no terms.  If
353 evidence of such a type is presented, then we can derive any type, which
354 expresses absurdity.  Conversely, truth ($\top$) is the type with just one
355 trivial element ($\lcunit$).
356
357 \newcommand{\lcinl}{\lccon{inl}\appsp}
358 \newcommand{\lcinr}{\lccon{inr}\appsp}
359 \newcommand{\lccase}[3]{\lcsyn{case}\appsp#1\appsp\lcsyn{of}\appsp#2\appsp#3}
360 \newcommand{\lcfst}{\lcfun{fst}\appsp}
361 \newcommand{\lcsnd}{\lcfun{snd}\appsp}
362 \newcommand{\orint}{\vee I_{1,2}}
363 \newcommand{\orintl}{\vee I_{1}}
364 \newcommand{\orintr}{\vee I_{2}}
365 \newcommand{\orel}{\vee E}
366 \newcommand{\andint}{\wedge I}
367 \newcommand{\andel}{\wedge E_{1,2}}
368 \newcommand{\botel}{\bot E}
369 \newcommand{\lcabsurd}{\lcfun{absurd}\appsp}
370 \newcommand{\lcabsurdd}[1]{\lcfun{absurd}_{#1}\appsp}
371
372
373 \begin{center}
374   \axname{syntax}
375   $$
376   \begin{array}{rcl}
377     \termsyn & ::= & \dotsb \\
378              &  |  & \lcinl \termsyn \separ \lcinr \termsyn \separ \lccase{\termsyn}{\termsyn}{\termsyn} \\
379              &  |  & (\termsyn , \termsyn) \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
380              &  |  & \lcunit \\
381     \tysyn & ::= & \dotsb \separ \tysyn \vee \tysyn \separ \tysyn \wedge \tysyn \separ \bot \separ \top
382   \end{array}
383   $$
384 \end{center}
385 \begin{center}
386   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
387   \begin{prooftree}
388     \AxiomC{$\Gamma \vdash \termt : \tya$}
389     \RightLabel{$\orint$}
390     \UnaryInfC{$\Gamma \vdash \lcinl \termt : \tya \vee \tyb$}
391     \noLine
392     \UnaryInfC{$\Gamma \vdash \lcinr \termt : \tyb \vee \tya$}
393   \end{prooftree}
394   \begin{prooftree}
395     \AxiomC{$\Gamma \vdash \termt : \tya \vee \tyb$}
396     \AxiomC{$\Gamma \vdash \termm : \tya \tyarr \tyc$}
397     \AxiomC{$\Gamma \vdash \termn : \tyb \tyarr \tyc$}
398     \RightLabel{$\orel$}
399     \TrinaryInfC{$\Gamma \vdash \lccase{\termt}{\termm}{\termn} : \tyc$}
400   \end{prooftree}
401
402   \begin{tabular}{c c}
403     \AxiomC{$\Gamma \vdash \termt : \tya$}
404     \AxiomC{$\Gamma \vdash \termm : \tyb$}
405     \RightLabel{$\andint$}
406     \BinaryInfC{$\Gamma \vdash (\tya , \tyb) : \tya \wedge \tyb$}
407     \DisplayProof
408     &
409     \AxiomC{$\Gamma \vdash \termt : \tya \wedge \tyb$}
410     \RightLabel{$\andel$}
411     \UnaryInfC{$\Gamma \vdash \lcfst \termt : \tya$}
412     \noLine
413     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb$}
414     \DisplayProof
415   \end{tabular}
416
417   \vspace{0.5cm}
418
419   \begin{tabular}{c c}
420     \AxiomC{$\Gamma \vdash \termt : \bot$}
421     \RightLabel{$\botel$}
422     \UnaryInfC{$\Gamma \vdash \lcabsurdd{\tya} \termt : \tya$}
423     \DisplayProof
424     &
425     \AxiomC{}
426     \RightLabel{$\top I$}
427     \UnaryInfC{$\Gamma \vdash \lcunit : \top$}
428     \DisplayProof
429   \end{tabular}
430 \end{center}
431 \begin{center}
432   \axdesc{reduction}{\termsyn \bred \termsyn}
433   \begin{eqnarray*}
434     \lccase{(\lcinl \termt)}{\termm}{\termn} & \bred & \app{\termm}{\termt} \\
435     \lccase{(\lcinr \termt)}{\termm}{\termn} & \bred & \app{\termn}{\termt} \\
436     \lcfst (\termt , \termm)                 & \bred & \termt \\
437     \lcsnd (\termt , \termm)                 & \bred & \termm
438   \end{eqnarray*}
439 \end{center}
440
441 With these rules, our STLC now looks remarkably similar in power and use to the
442 natural deduction we already know.  $\neg A$ can be expressed as $A \tyarr
443 \bot$.  However, there is an important omission: there is no term of the type $A
444 \vee \neg A$ (excluded middle), or equivalently $\neg \neg A \tyarr A$ (double
445 negation), or indeed any term with a type equivalent to those.
446
447 This has a considerable effect on our logic and it's no coincidence, since there
448 is no obvious computational behaviour for laws like the excluded middle.
449 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
450 and all the systems analysed will have this characteristic since they build on
451 the foundation of the STLC\footnote{There is research to give computational
452   behaviour to classical logic, but I will not touch those subjects.}.
453
454 Finally, going back to our $\lcsyn{fix}$ combinator, it's now easy to see how
455 we would want to exclude such a thing if we want to use STLC as a logic, since
456 it allows us to prove everything: $(\lcfix{x : \tya}{x}) : \tya$ clearly works
457 for any $A$!  This is a crucial point: in general we wish to have systems that
458 do not let the user devise a term of type $\bot$, otherwise our logic will be
459 unsound\footnote{Obviously such a term can be present under a $\lambda$.}.
460
461 \subsection{Extending the STLC}
462
463 \newcommand{\lctype}{\lccon{Type}}
464 \newcommand{\lcite}[3]{\lcsyn{if}\appsp#1\appsp\lcsyn{then}\appsp#2\appsp\lcsyn{else}\appsp#3}
465 \newcommand{\lcbool}{\lccon{Bool}}
466 \newcommand{\lcforallz}[2]{\forall #1 \absspace.\absspace #2}
467 \newcommand{\lcforall}[3]{(#1 : #2) \tyarr #3}
468 \newcommand{\lcexists}[3]{(#1 : #2) \times #3}
469
470 The STLC can be made more expressive in various ways.  Henk Barendregt
471 succinctly expressed geometrically how we can expand our type system:
472
473 $$
474 \xymatrix@!0@=1.5cm{
475   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
476   & & \lambda C \ar@{-}[dd]
477   \\
478   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
479   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
480   \\
481   & \lambda\underline\omega \ar@{-}'[r][rr]
482   & & \lambda P\underline\omega
483   \\
484   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
485   & & \lambda P \ar@{-}[ur]
486 }
487 $$
488 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
489 3 dimensions:
490 \begin{description}
491 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
492   types in our type signatures: $(\abs{A : \lctype}{\abs{x : A}{x}}) :
493   \lcforallz{A}{A \tyarr A}$.  The first and most famous instance of this idea
494   has been System F.  This gives us a form of polymorphism and has been wildly
495   successful, also thanks to a well known inference algorithm for a restricted
496   version of System F known as Hindley-Milner.  Languages like Haskell and SML
497   are based on this discipline.
498 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
499   type operators: $(\abs{A : \lctype}{\abs{R : \lctype}{(A \to R) \to R}}) :
500   \lctype \to \lctype \to \lctype$.
501 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
502   types', give great expressive power: $(\abs{x : \lcbool}{\lcite{x}{\mathbb{N}}{\mathbb{Q}}}) : \lcbool \to \lctype$.
503 \end{description}
504
505 All the systems preserve the properties that make the STLC well behaved.  The
506 system we are going to focus on, Intuitionistic Type Theory, has all of the
507 above additions, and thus would sit where $\lambda{C}$ sits in the
508 `$\lambda$-cube'.
509
510 \section{Intuitionistic Type Theory and dependent types}
511 \label{sec:itt}
512
513 \newcommand{\lcset}[1]{\lccon{Type}_{#1}}
514 \newcommand{\lcsetz}{\lccon{Type}}
515 \newcommand{\defeq}{\cong}
516
517 \subsection{A Bit of History}
518
519 Logic frameworks and programming languages based on type theory have a long
520 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
521 but then revised it since the original version was too impredicative and thus
522 inconsistent\footnote{In the early version $\lcsetz : \lcsetz$, see section
523   \ref{sec:core-tt} for an explanation on why this causes problems.}.  For this
524 reason he gave a revised and consistent definition later \citep{Martin-Lof1984}.
525
526 A related development is the polymorphic $\lambda$-calculus, and specifically
527 the previously mentioned System F, which was developed independently by Girard
528 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
529 fact is that while System F is impredicative it is still consistent and strongly
530 normalising.  \cite{Coquand1986} further extended this line of work with the
531 Calculus of Constructions (CoC).
532
533 \subsection{A Core Type Theory}
534 \label{sec:core-tt}
535
536 The calculus I present follows the exposition in \citep{Thompson1991}, and is
537 quite close to the original formulation of predicative ITT as found in
538 \citep{Martin-Lof1984}.
539
540 \begin{center}
541   \axname{syntax}
542   \begin{eqnarray*}
543   \termsyn & ::= & x \\
544          &  |  & \lcforall{x}{\termsyn}{\termsyn} \separ \abs{x : \termsyn}{\termsyn} \separ \app{\termsyn}{\termsyn} \\
545          &  |  & \lcexists{x}{\termsyn}{\termsyn} \separ (\termsyn , \termsyn)_{x.\termsyn} \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
546          &  |  & \bot \separ \lcabsurd_{\termsyn} \termsyn \\
547          &  |  & \lcset{n} \\
548    n     & \in & \mathbb{N}
549  \end{eqnarray*}
550
551   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
552
553   \vspace{0.5cm}
554
555   \begin{tabular}{c c c}
556     \AxiomC{\phantom{1em}}
557     \UnaryInfC{$\Gamma;x : \tya \vdash x : \tya$}
558     \DisplayProof
559     &
560     \AxiomC{$\Gamma \vdash \termt : \tya$}
561     \AxiomC{$\tya \defeq \tyb$}
562     \BinaryInfC{$\Gamma \vdash \termt : \tyb$}
563     \DisplayProof
564     &
565     \AxiomC{$\Gamma \vdash \termt : \bot$}
566     \UnaryInfC{$\Gamma \vdash \lcabsurdd{\tya} \termt : \tya$}
567     \DisplayProof
568   \end{tabular}
569
570   \vspace{0.5cm}
571
572   \begin{tabular}{c c}
573     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
574     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \lcforall{x}{\tya}{\tyb}$}
575     \DisplayProof
576     &
577     \AxiomC{$\Gamma \vdash \termt : \lcforall{x}{\tya}{\tyb}$}
578     \AxiomC{$\Gamma \vdash \termm : \tya$}
579     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb[\termm ]$}
580     \DisplayProof
581   \end{tabular}
582
583   \vspace{0.5cm}
584
585   \begin{tabular}{c c}
586     \AxiomC{$\Gamma \vdash \termt : \tya$}
587     \AxiomC{$\Gamma \vdash \termm : \tyb[\termt ]$}
588     \BinaryInfC{$\Gamma \vdash (\termt, \termm)_{x.\tyb} : \lcexists{x}{\tya}{\tyb}$}
589     \noLine
590     \UnaryInfC{$\phantom{1em}$}
591     \DisplayProof
592     &
593     \AxiomC{$\Gamma \vdash \termt: \lcexists{x}{\tya}{\tyb}$}
594     \UnaryInfC{$\hspace{0.7cm} \Gamma \vdash \lcfst \termt : \tya \hspace{0.7cm}$}
595     \noLine
596     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb[\lcfst \termt ]$}
597     \DisplayProof
598   \end{tabular}
599
600   \vspace{0.5cm}
601
602   \begin{tabular}{c c}
603     \AxiomC{$\phantom{1em}$}
604     \UnaryInfC{$\Gamma \vdash \lcset{n} : \lcset{n + 1}$}
605     \noLine
606     \UnaryInfC{$\phantom{1em}$}
607     \DisplayProof
608     &
609     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
610     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
611     \BinaryInfC{$\Gamma \vdash \lcforall{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
612     \noLine
613     \UnaryInfC{$\Gamma \vdash \lcexists{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
614     \DisplayProof
615   \end{tabular}
616
617   \vspace{0.5cm}
618
619   \axdesc{reduction}{\termsyn \bred \termsyn}
620   \begin{eqnarray*}
621     \app{(\abs{x}{\termt})}{\termm} & \bred & \termt[\termm ] \\
622     \lcfst (\termt, \termm) & \bred & \termt \\
623     \lcsnd (\termt, \termm) & \bred & \termm
624   \end{eqnarray*}
625 \end{center}
626
627 When showing examples types will be omitted when this can be done without loss
628 of clarity, for example $\abs{x}{x}$ in place of $\abs{A : \lcsetz}{\abs{x :
629     A}{x}}$.  I will also use $\lcsetz$ as $\lcset{0}$.
630
631 There are a lot of new factors at play here. The first thing to notice is that
632 the separation between types and terms is gone.  All we have is terms, that
633 include both values (terms of type $\lcset{0}$) and types (terms of type
634 $\lcset{n}$, with $n > 0$).  This change is reflected in the typing rules.
635 While in the STLC values and types are kept well separated (values never go
636 `right of the colon'), in ITT types can freely depend on values.
637
638 This relation is expressed in the typing rules for $\tyarr$ and $\times$: if a
639 function has type $\lcforall{x}{\tya}{\tyb}$, $\tyb$ can depend on $x$.
640 Examples will make this clearer once some base types are added in section
641 \ref{sec:base-types}.
642
643 $\tyarr$ and $\times$ are at the core of the machinery of ITT:
644
645 \begin{description}
646 \item[`forall' ($\tyarr$)] is a generalisation of $\tyarr$ in the STLC and
647   expresses universal quantification in our logic.  It is often written with a
648   syntax closer to logic, e.g. $\forall x : \tya. \tyb$.  In the
649   literature this is also known as `dependent product' and shown as $\Pi$,
650   following the interpretation of functions as infinitary products. I will just
651   call it `dependent function', reserving `product' for $\exists$.
652
653 \item[`exists' ($\times$)] is a generalisation of $\wedge$ in the extended STLC
654   of section \ref{sec:curry-howard}, and thus I will call it `dependent
655   product'.  Like $\wedge$, it is formed by providing a pair of things.  In our
656   logic, it represents existential quantification.  Similarly to $\tyarr$, it is
657   always written as $\exists x : \tya. \tyb$.
658
659   For added confusion, in the literature that calls forall $\Pi$, $\exists$ is
660   often named `dependent sum' and shown as $\Sigma$.  This is following the
661   interpretation of $\exists$ as a generalised, infinitary $\vee$, where the
662   first element of the pair is the `tag' that determines which type the second
663   element will have.
664 \end{description}
665
666 Another thing to notice is that types are very `first class': we are free to
667 create functions that accept and return types.  For this reason we define
668 $\defeq$ as the smallest equivalence relation extending $\bredc$, where $\bredc$
669 is the reflexive transitive closure of $\bred$; and we treat types that are
670 relate according to $\defeq$ as the same.  Another way of seeing $\defeq$ is
671 this: when we want to compare two types for equality, we reduce them as far as
672 possible and then check if they are equal\footnote{Note that when comparing
673   terms we do it up to $\alpha$-renaming.  That is, we do not consider
674   relabelling of variables as a difference - for example $\abs{x : A}{x} \defeq
675   \abs{y : A}{y}$.}.  This works since not only each term has a normal form (ITT
676 is strongly normalising), but the normal form is also unique; or in other words
677 $\bred$ is confluent (if $\termt \bredc \termm$ and $\termt \bredc \termn$, then
678 there is a $p$ such that $\termm \bredc \termp$ and $\termn \bredc \termp$).
679 This measure makes sure that, for instance, $\app{(\abs{x :
680     \lctype}{x})}{\lcbool} \defeq \lcbool$.  The theme of equality is central
681 and will be analysed better later.
682
683 The theory presented is \emph{stratified}.  We have a hierarchy of types
684 $\lcset{0} : \lcset{1} : \lcset{2} : \dots$, so that there is no `type of all
685 types', and our theory is predicative.  Type-formers like $\tyarr$ and $\times$
686 take the least upper bound $\sqcup$ of the contained types. The layers of the
687 hierarchy are called `universes'.  Theories where $\lcsetz : \lcsetz$ are
688 inconsistent due to Girard's paradox \citep{Hurkens1995}, and thus lose their
689 well-behavedness.  Some impredicativity sometimes has its place, either because
690 the theory retain good properties (normalization, consistency, etc.) anyway,
691 like in System F and CoC; or because we are at a stage at which we do not care.
692
693 Note that the Curry-Howard correspondence runs through ITT as it did with the
694 STLC with the difference that ITT corresponds to an higher order propositional
695 logic.
696
697 \subsection{Base Types}
698 \label{sec:base-types}
699
700 \newcommand{\lctrue}{\lccon{true}}
701 \newcommand{\lcfalse}{\lccon{false}}
702 \newcommand{\lcw}[3]{\lccon{W} #1 : #2 \absspace.\absspace #3}
703 \newcommand{\lcnode}[4]{#1 \lhd_{#2 . #3} #4}
704 \newcommand{\lcnodez}[2]{#1 \lhd #2}
705 \newcommand{\lcited}[5]{\lcsyn{if}\appsp#1/#2\appsp.\appsp#3\appsp\lcsyn{then}\appsp#4\appsp\lcsyn{else}\appsp#5}
706 \newcommand{\lcrec}[4]{\lcsyn{rec}\appsp#1/#2\appsp.\appsp#3\appsp\lcsyn{with}\appsp#4}
707 \newcommand{\lcrecz}[2]{\lcsyn{rec}\appsp#1\appsp\lcsyn{with}\appsp#2}
708 \newcommand{\AxiomL}[1]{\Axiom$\fCenter #1$}
709 \newcommand{\UnaryInfL}[1]{\UnaryInf$\fCenter #1$}
710
711 While the ITT presented is a fairly complete logic, it is not that useful for
712 programming.  If we wish to make it better, we can add some base types to
713 represent the data structures we know and love, such as numbers, lists, and
714 trees.  Apart from some unsurprising data types, we introduce $\lccon{W}$, a
715 very general tree-like structure useful to represent inductively defined types
716 and that will allow us to express structural recursion in an equally general
717 manner.
718
719 \begin{center}
720   \axname{syntax}
721   \begin{eqnarray*}
722   \termsyn & ::= & \dots \\
723            &  |  & \top \separ \lcunit \\
724            &  |  & \lcbool \separ \lctrue \separ \lcfalse \separ \lcited{\termsyn}{x}{\termsyn}{\termsyn}{\termsyn} \\
725            &  |  & \lcw{x}{\termsyn}{\termsyn} \separ \lcnode{\termsyn}{x}{\termsyn}{\termsyn} \separ \lcrec{\termsyn}{x}{\termsyn}{\termsyn}
726  \end{eqnarray*}
727
728   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
729
730   \vspace{0.5cm}
731
732   \begin{tabular}{c c c}
733     \AxiomC{}
734     \UnaryInfC{$\hspace{0.2cm}\Gamma \vdash \top : \lcset{0} \hspace{0.2cm}$}
735     \noLine
736     \UnaryInfC{$\Gamma \vdash \lcbool : \lcset{0}$}
737     \DisplayProof
738     &
739     \AxiomC{}
740     \UnaryInfC{$\Gamma \vdash \lcunit : \top$}
741     \noLine
742     \UnaryInfC{\phantom{1em}}
743     \DisplayProof
744     &
745     \AxiomC{}
746     \UnaryInfC{$\Gamma \vdash \lctrue : \lcbool$}
747     \noLine
748     \UnaryInfC{$\Gamma \vdash \lcfalse : \lcbool$}
749     \DisplayProof
750   \end{tabular}
751
752   \vspace{0.5cm}
753
754   \begin{tabular}{c}
755     \AxiomC{$\Gamma \vdash \termt : \lcbool$}
756     \AxiomC{$\Gamma \vdash \termm : \tya[\lctrue]$}
757     \AxiomC{$\Gamma \vdash \termn : \tya[\lcfalse]$}
758     \TrinaryInfC{$\Gamma \vdash \lcited{\termt}{x}{\tya}{\termm}{\termn} : \tya[\termt]$}
759     \DisplayProof
760   \end{tabular}
761
762   \vspace{0.5cm}
763
764   \begin{tabular}{c}
765     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
766     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
767     \BinaryInfC{$\Gamma \vdash \lcw{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
768     \DisplayProof
769   \end{tabular}
770
771   \vspace{0.5cm}
772
773   \begin{tabular}{c}
774     \AxiomC{$\Gamma \vdash \termt : \tya$}
775     \AxiomC{$\Gamma \vdash \termf : \tyb[\termt ] \tyarr \lcw{x}{\tya}{\tyb}$}
776     \BinaryInfC{$\Gamma \vdash \lcnode{\termt}{x}{\tyb}{\termf} : \lcw{x}{\tya}{\tyb}$}
777     \DisplayProof
778   \end{tabular}
779
780   \vspace{0.5cm}
781
782   \begin{tabular}{c}
783     \AxiomC{$\Gamma \vdash \termt: \lcw{x}{\tya}{\tyb}$}
784     \noLine
785     \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}]}}$}
786     \UnaryInfC{$\Gamma \vdash \lcrec{\termt}{x}{\tyc}{\termp} : \tyc[\termt]$}
787     \DisplayProof
788   \end{tabular}
789
790   \vspace{0.5cm}
791
792   \axdesc{reduction}{\termsyn \bred \termsyn}
793   \begin{eqnarray*}
794     \lcited{\lctrue}{x}{\tya}{\termt}{\termm} & \bred & \termt \\
795     \lcited{\lcfalse}{x}{\tya}{\termt}{\termm} & \bred & \termm \\
796     \lcrec{\lcnodez{\termt}{\termf}}{x}{\tya}{\termp} & \bred & \app{\app{\app{\termp}{\termt}}{\termf}}{(\abs{\termm}{\lcrec{\app{f}{\termm}}{x}{\tya}{\termp}})}
797   \end{eqnarray*}
798 \end{center}
799
800 The introduction and elimination for $\top$ and $\lcbool$ are unsurprising.
801 Note that in the $\lcite{\dotsb}{\dotsb}{\dotsb}$ construct the type of the
802 branches are dependent on the value of the conditional.
803
804 The rules for $\lccon{W}$, on the other hand, are quite an eyesore.  The idea
805 behind $\lccon{W}$ types is to build up `trees' where the number of `children'
806 of each node is dependent on the value (`shape') in the node.  This is captured
807 by the $\lhd$ constructor, where the argument on the left is the value, and the
808 argument on the right is a function that returns a child for each possible value
809 of $\tyb[\text{node value}]$, if $\lcw{x}{\tya}{\tyb}$.  The recursor
810 $\lcrec{\termt}{x}{\tyc}{\termp}$ uses $p$ to inductively prove that
811 $\tyc[\termt]$ holds.
812
813 \subsection{Some examples}
814
815 Now we can finally provide some meaningful examples.  Apart from omitting types,
816 I will also use some abbreviations:
817 \begin{itemize}
818   \item $\_\mathit{operator}\_$ to define infix operators
819   \item $\abs{x\appsp y\appsp z : \tya}{\dotsb}$ to define multiple abstractions at the same
820     time
821 \end{itemize}
822
823 \subsubsection{Sum types}
824
825 We would like to recover our sum type, or disjunction, $\vee$.  This is easily
826 achieved with $\times$:
827 \[
828 \begin{array}{rcl}
829   \_\vee\_ & = &  \abs{\tya\appsp\tyb : \lcsetz}{\lcexists{x}{\lcbool}{\lcite{x}{\tya}{\tyb}}} \\
830   \lcinl   & = & \abs{x}{(\lctrue, x)} \\
831   \lcinr   & = & \abs{x}{(\lcfalse, x)} \\
832   \lcfun{case} & = & \abs{x : \tya \vee \tyb}{\abs{f : \tya \tyarr \tyc}{\abs{g : \tyb \tyarr \tyc}{ \\
833            &   & \hspace{0.5cm} \app{(\lcited{\lcfst x}{b}{(\lcite{b}{A}{B}) \tyarr C}{f}{g})}{(\lcsnd x)}}}}
834 \end{array}
835 \]
836 What is going on here?  We are using $\exists$ with $\lcbool$ as a tag, so that
837 we can choose between one of two types in the second element.  In
838 $\lcfun{case}$ we use $\lcite{\lcfst x}{\dotsb}{\dotsb}$ to discriminate on the
839 tag, that is, the first element of $x : \tya \vee \tyb$.  If the tag is true,
840 then we know that the second element is of type $\tya$, and we will apply $f$.
841 The same applies to the other branch, with $\tyb$ and $g$.
842
843 \subsubsection{Naturals}
844
845 Now it's time to showcase the power of $\lccon{W}$ types.
846 \begin{eqnarray*}
847   \lccon{Nat}  & = & \lcw{b}{\lcbool}{\lcite{b}{\top}{\bot}}  \\
848   \lccon{zero} & = & \lcfalse \lhd \abs{z}{\lcabsurd z} \\
849   \lccon{suc}  & = & \abs{n}{(\lctrue \lhd \abs{\_}{n})}  \\
850   \lcfun{plus} & = & \abs{x\appsp y}{\lcrecz{x}{(\abs{b}{\lcite{b}{\abs{\_\appsp f}{\app{\lccon{suc}}{(\app{f}{\lcunit})}}}{\abs{\_\appsp\_}{y}}})}}
851 \end{eqnarray*}
852 Here we encode natural numbers through $\lccon{W}$ types.  Each node contains a
853 $\lcbool$.  If the $\lcbool$ is $\lcfalse$, then there are no children, since we
854 have a function $\bot \tyarr \lccon{Nat}$: this is our $0$.  If it's $\lctrue$,
855 we need one child only: the predecessor.  We recurse giving $\lcsyn{rec}$ a
856 function that handles the `base case' 0 when the $\lcbool$ is $\lctrue$, and the
857 inductive case otherwise.
858
859 We postpone more complex examples after the introduction of inductive families
860 and pattern matching, since $\lccon{W}$ types get unwieldy very quickly.
861
862 \subsection{Propositional Equality}
863 \label{sec:propeq}
864
865 We can finally introduce one of the central subjects of my project:
866 propositional equality.
867
868 \newcommand{\lceq}[3]{#2 =_{#1} #3}
869 \newcommand{\lceqz}[2]{#1 = #2}
870 \newcommand{\lcrefl}[2]{\lccon{refl}_{#1}\appsp #2}
871 \newcommand{\lcsubst}[3]{\lcfun{subst}\appsp#1\appsp#2\appsp#3}
872
873 \begin{center}
874   \axname{syntax}
875   \begin{eqnarray*}
876   \termsyn & ::= & ... \\
877            &  |  & \lceq{\termsyn}{\termsyn}{\termsyn} \separ \lcrefl{\termsyn}{\termsyn} \separ \lcsubst{\termsyn}{\termsyn}{\termsyn}
878  \end{eqnarray*}
879
880   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
881
882   \vspace{0.5cm}
883
884   \begin{tabular}{c c}
885     \AxiomC{$\Gamma \vdash \termt : \tya$}
886     \UnaryInfC{$\Gamma \vdash \lcrefl{\tya}{\termt} : \lceq{\tya}{\termt}{\termt}$}
887     \DisplayProof
888     &
889     \AxiomC{$\Gamma \vdash \termp : \lceq{\tya}{\termt}{\termm}$}
890     \AxiomC{$\Gamma \vdash \tyb : \tya \tyarr \lcsetz$}
891     \AxiomC{$\Gamma \vdash \termn : \app{\tyb}{\termt}$}
892     \TrinaryInfC{$\Gamma \vdash \lcsubst{\termp}{\tyb}{\termn} : \app{\tyb}{\termm}$}
893     \DisplayProof
894   \end{tabular}
895
896   \vspace{0.5cm}
897
898   \axdesc{reduction}{\termsyn \bred \termsyn}
899   \begin{eqnarray*}
900     \lcsubst{(\lcrefl{\tya}{\termt})}{\tyb}{\termm} \bred \termm
901   \end{eqnarray*}
902 \end{center}
903
904 Propositional equality internalises equality as a type.  The only way to
905 introduce an equality proof is by reflexivity ($\lccon{refl}$).  The eliminator
906 is in the style of `Leibnitz's law' of equality: `equals can be substituted for
907 equals' ($\lcfun{subst}$).  The computation rule refers to the fact that every
908 proof of equality will be a $\lccon{refl}$.  We can use $\neg
909 (\lceq{\tya}{x}{y})$ to express inequality.
910
911 These elements conclude our presentation of a `core' type theory.  For an
912 extended example of a similar theory in use see Section 6.2 of
913 \citep{Thompson1991}\footnote{Note that while I attempted to formalise the proof
914   in Agda, I found a bug in the book!  See the errata for details:
915   \url{http://www.cs.kent.ac.uk/people/staff/sjt/TTFP/errata.html}.}.  The above
916 language and examples have been codified in Agda\footnote{More on Agda in the
917   next section.}, see appendix \ref{app:agda-code}.
918
919 \section{A more useful language}
920 \label{sec:practical}
921
922 While our core type theory equipped with $\lccon{W}$ types is very usefully
923 conceptually as a simple but complete language, things get messy very fast,
924 since handling $\lccon{W}$ types directly is incredibly cumbersome.  In this
925 section I will present the elements that are usually included in theorem provers
926 or programming languages to make them usable by mathematicians or programmers.
927
928 All the features presented are present in the second version of the Agda system
929 \citep{Norell2007, Bove2009}.  Agda follows a tradition of theorem provers based
930 on ITT with inductive families and pattern matching.  The closest relative of
931 Agda, apart from previous versions, is Epigram \citep{McBride2004, EpigramTut}.
932 Coq is another notable theorem prover based on the same concepts, and the first
933 and most popular \citep{Coq}.
934
935 The main difference between Coq and Agda/Epigram is that the former focuses on
936 theorem proving, while the latter also want to be useful as functional
937 programming languages, while still offering good tools to express
938 mathematics\footnote{In fact, currently, Agda is used almost exclusively to
939   express mathematics, rather than to program.}.  This is reflected in a series
940 of differences that I will not discuss here (most notably the absence of tactics
941 but better support for pattern matching in Agda/Epigram).  Every feature will be
942 presented as it is Agda.
943
944 As previously, all the examples presented have been codified in Agda, see
945 appendix \ref{app:agda-code}.
946
947 \subsection{Type inference}
948
949 The theory I presented is fully explicit in the sense that the user has to
950 specify every type when forming abstractions, products, etc.
951
952 For the programmer used to Hindley-Milner as in Haskell and SML (and for any
953 human being), this is a great burden. Complete inference is undecidable - which
954 is hardly surprising considering the role that types play - but partial
955 inference in the style of \cite{Pierce2000}, also called `bidirectional type
956 checking' in this context, will have to be deployed in a practical system.
957
958 Agda gives users an explicit way to indicate which fields should be implicit, by
959 wrapping them in curly braces in type signatures: $\{A : \lcsetz\} \tyarr
960 \dotsb$.  It also allows to omit types of arguments altimeter, if they can be
961 inferred by other arguments: $\{A\} \tyarr (x : A) \tyarr \dotsb$.
962
963 \subsection{Inductive families}
964 \label{sec:inductive-families}
965
966 \newcommand{\lcdata}[1]{\lcsyn{data}\appsp #1}
967 \newcommand{\lcdb}{\ |\ }
968 \newcommand{\lcind}{\hspace{0.2cm}}
969 \newcommand{\lcwhere}{\appsp \lcsyn{where}}
970 \newcommand{\lclist}[1]{\app{\lccon{List}}{#1}}
971 \newcommand{\lcnat}{\lccon{Nat}}
972 \newcommand{\lcvec}[2]{\app{\app{\lccon{Vec}}{#1}}{#2}}
973
974 Inductive families were first introduced by \cite{Dybjer1991}.  For the reader
975 familiar with the recent developments present in the GHC compiler for Haskell,
976 inductive families will look similar to GADTs (Generalised Abstract Data Types)
977 \citep[Section 7.4.7]{GHC}.
978 Haskell-style data types provide \emph{parametric polymorphism}, so that we can
979 define types that range over type parameters:
980 \begin{lstlisting}
981 List a = Nil | Cons a (List a)
982 \end{lstlisting}
983 In this way we define the \texttt{List} type once while allowing elements to be
984 of any type.  In Haskell \texttt{List} will be a type constructor of kind
985 \texttt{* -> *}, while \texttt{Nil :: List a} and \texttt{Cons :: a -> List a ->
986   List a}\footnote{Note that the \texttt{a}s are implicitly quantified type
987   variables.}.
988
989 Inductive families bring this concept one step further by allowing some of the
990 parameters to be constrained by constructors.  We call these `variable'
991 parameters `indices'.  For example we might define natural numbers
992 \[
993 \begin{array}{l}
994 \lcdata{\lcnat} : \lcsetz \lcwhere \\
995   \begin{array}{l@{\ }l}
996     \lcind \lccon{zero} &: \lcnat \\
997     \lcind \lccon{suc}  &: \lcnat \tyarr \lcnat
998   \end{array}
999 \end{array}
1000 \]
1001 And then define a type for lists indexed by length:
1002 \[
1003 \begin{array}{l}
1004 \lcdata{\lccon{Vec}}\appsp (A : \lcsetz) : \lcnat \tyarr \lcsetz \lcwhere \\
1005   \begin{array}{l@{\ }l}
1006     \lcind \lccon{nil} &: \lcvec{A}{\lccon{zero}} \\
1007     \lcind \_::\_ &: \{n : \lccon{Nat}\} \tyarr A \tyarr \lcvec{A}{n} \tyarr \lcvec{A}{\app{(\lccon{suc}}{n})}
1008   \end{array}
1009 \end{array}
1010 \]
1011 Note that contrary to the Haskell ADT notation, with inductive families we
1012 explicitly list the types for the type and data constructors.  In $\lccon{Vec}$,
1013 $A$ is a parameter (same across all constructors) while the $\lcnat$ is an
1014 index.  In our syntax, in the $\lcsyn{data}$ declaration, things to the left of
1015 the colon are parameters, while on the right we can define the type of indices.
1016 Also note that the parameters' identifiers will be in scope across all
1017 constructors, while indices' won't.
1018
1019 In this $\lccon{Vec}$ example, when we form a new list the length is
1020 $\lccon{zero}$.  When we append a new element to an existing list of length $n$,
1021 the new list is of length $\app{\lccon{suc}}{n}$, that is, one more than the
1022 previous length.  Once $\lccon{Vec}$ is defined we can do things much more
1023 safely than with normal lists.  For example, we can define an $\lcfun{head}$
1024 function that returns the first element of the list:
1025 \[
1026 \begin{array}{l}
1027 \lcfun{head} : \{A\ n\} \tyarr \lcvec{A}{(\app{\lccon{suc}}{n})} \tyarr A
1028 \end{array}
1029 \]
1030 Note that we will not be able to provide this function with a
1031 $\lcvec{A}{\lccon{zero}}$, since it needs an index which is a successor of some
1032 number.
1033
1034 \newcommand{\lcfin}[1]{\app{\lccon{Fin}}{#1}}
1035
1036 If we wish to index a $\lccon{Vec}$ safely, we can define an inductive family
1037 $\lcfin{n}$, which represents the family of numbers smaller than a given number
1038 $n$:
1039 \[
1040 \begin{array}{l}
1041   \lcdata{\lccon{Fin}} : \lcnat \tyarr \lcsetz \lcwhere \\
1042   \begin{array}{l@{\ }l}
1043     \lcind \lccon{fzero} &: \{n\} \tyarr \lcfin{(\app{\lccon{suc}}{n})} \\
1044     \lcind \lccon{fsuc}  &: \{n\} \tyarr \lcfin{n} \tyarr \lcfin{(\app{\lccon{suc}}{n})}
1045   \end{array}
1046 \end{array}
1047 \]
1048 $\lccon{fzero}$ is smaller than any number apart from $\lccon{zero}$.  Given
1049 the family of numbers smaller than $n$, we can produce the family of numbers
1050 smaller than $\app{\lccon{suc}}{n}$.  Now we can define an `indexing' operation
1051 for our $\lccon{Vec}$:
1052 \[
1053 \begin{array}{l}
1054 \_\lcfun{!!}\_ : \{A\ n\} \tyarr \lcvec{A}{n} \tyarr \lcfin{n} \tyarr A
1055 \end{array}
1056 \]
1057 In this way we will be sure that the number provided as an index will be smaller
1058 than the length of the $\lccon{Vec}$ provided.
1059
1060 \subsubsection{Computing with inductive families}
1061
1062 I have carefully avoided to define the functions that I mentioned as example,
1063 since one question still is unanswered: `how do we work with inductive
1064 families'?  The most basic approach is to work with eliminators, that can be
1065 automatically provided by the programming language for each data type defined.
1066 For example the induction principle on natural numbers will serve as an
1067 eliminator for $\lcnat$:
1068 \[
1069 \begin{array}{l@{\ } c@{\ } l}
1070   \lcfun{NatInd} & : & (A : \lcsetz) \tyarr \\
1071                  &   & A \tyarr (\lcnat \tyarr A \tyarr A) \tyarr \\
1072                  &   & \lcnat \tyarr A
1073 \end{array}
1074 \]
1075
1076 That is, if we provide an $A$ (base case), and then given a number and an $A$ we
1077 give the next $A$ (inductive step), then an $A$ can be computed for every
1078 number.  This can be expressed easily in Haskell:
1079 \begin{lstlisting}
1080 data Nat = Zero | Suc Nat
1081 natInd :: a -> (Nat -> a -> a) -> Nat -> a
1082 natInd z _  Zero   = z
1083 natInd z f (Suc n) = f n (natInd z f n)
1084 \end{lstlisting}
1085 However, in a dependent setting, we can do better:
1086 \[
1087 \begin{array}{l@{\ } c@{\ } l}
1088   \lcfun{NatInd} & : & (P : \lcnat \tyarr \lcsetz) \tyarr \\
1089                  &   & \app{P}{\lccon{zero}} \tyarr ((n : \lcnat) \tyarr \app{P}{n} \tyarr \app{P}{(\app{\lccon{suc}}{n})}) \tyarr \\
1090                  &   & (n : \lcnat) \tyarr \app{P}{n}
1091 \end{array}
1092 \]
1093 This expresses the fact that the resulting type can be dependent on the number.
1094 In other words, we are proving that $P$ holds for all $n : \lcnat$.
1095
1096 Naturally a reduction rule will be associated with each eliminator:
1097 \[
1098 \begin{array}{l c l}
1099 \app{\app{\app{\app{\lcfun{NatInd}}{P}}{z}}{f}}{\lccon{zero}} & \bred & z \\
1100 \app{\app{\app{\app{\lcfun{NatInd}}{P}}{z}}{f}}{(\app{\lccon{suc}}{n})} & \bred & \app{\app{f}{n}}{(\app{\app{\app{\app{\lcfun{NatInd}}{P}}{z}}{f}}{n})}
1101 \end{array}
1102 \]
1103 Which echoes the \texttt{natInd} function defined in Haskell.  An extensive
1104 account on combinators and inductive families can be found in \citep{McBride2004}.
1105
1106 \subsubsection{Pattern matching and guarded recursion}
1107
1108 However, combinators are far more cumbersome to use than the techniques usually
1109 employed in functional programming: pattern matching and recursion.
1110 \emph{General} recursion (exemplified by the $\lcsyn{fix}$ combinator in section
1111 \ref{sec:stlc}) cannot be added if we want to keep our theory free of $\bot$.
1112 The common solution to this problem is to allow recursive calls only if the
1113 arguments are structurally smaller than what the function received, what is
1114 known as \emph{structural} recursion.  For example, if we have a $\lccon{Tree}$
1115 family with a $\lccon{node}\appsp l \appsp r$ (and maybe others) constructor,
1116 functions that work on $\lccon{Tree}$ will be able to make recursive calls on
1117 $l$ and $r$.
1118
1119 Pattern matching on the other hand gains considerable power with inductive
1120 families, since when we match a constructor we are gaining information on the
1121 indices of the family.  Thus matching constructors will enable us to restrict
1122 patterns of other arguments.
1123
1124 Following this discipline defining $\lcfun{head}$ becomes easy:
1125 \[
1126 \begin{array}{l}
1127 \lcfun{head} : \{A\ n\} \tyarr \lcvec{A}{(\app{\lccon{suc}}{n})} \tyarr A \\
1128 \lcfun{head}\appsp(x :: \_) = x
1129 \end{array}
1130 \]
1131 We need no case for $\lccon{nil}$, since the type checker knows that the
1132 equation $n = \lccon{zero} = \app{\lccon{suc}}{n'}$ cannot be satisfied.
1133
1134 More details on the implementations of inductive families can be found in
1135 \citep{McBride2004, Norell2007}.
1136
1137 \subsection{Friendlier universes}
1138
1139 Universes as presented in section \ref{sec:core-tt} are quite cumbersome to work
1140 with.  Consider the example where we define an inductive family for booleans and
1141 then we want to define an `if then else' eliminator:
1142 \[
1143 \begin{array}{l}
1144 \lcdata{\lcbool} : \lcsetz \lcwhere \\
1145   \begin{array}{l@{\ }l}
1146     \lcind \lccon{true}  &: \lcbool \\
1147     \lcind \lccon{false} &: \lcbool
1148   \end{array}\\
1149 \\
1150 \lcfun{ite} : \{A : \lcset{0}\} \tyarr \lcbool \tyarr A \tyarr A \tyarr A \\
1151 \lcfun{ite} \appsp \lccon{true} \appsp x \appsp \_ = x \\
1152 \lcfun{ite} \appsp \lccon{false} \appsp \_ \appsp x = x
1153 \end{array}
1154 \]
1155
1156 What if we want to use $\lcfun{ite}$ with types, for example $\lcfun{ite} \appsp
1157 b \appsp \lcnat \appsp \lccon{Bool}$?  Clearly $\lcnat$ is not of type
1158 $\lcset{0}$, so we'd have to redefine $\lcfun{ite}$ with exactly the same body,
1159 but different type signature.  The problem is even more evident with type
1160 families: for example our $\lccon{List}$ can only contain values, and if we want
1161 to build lists of types we need to define another identical, but differently
1162 typed, family.
1163
1164 One option is to have a \emph{cumulative} theory, where $\lcset{n} : \lcset{m}$
1165 iff $n < m$.  Then we can have a sufficiently large level in our type signature
1166 and forget about it.  Moreover, levels in this setting can be inferred
1167 mechanically \citep{Pollack1990}, and thus we can lift the burden of specifying
1168 universes from the user.  This is the approach taken by Epigram.
1169
1170 Another more expressive (but currently more cumbersome) way is to expose
1171 universes more, giving the user a way to quantify them and to take the least
1172 upper bound of two levels.  This is the approach taken by Agda, where for
1173 example we can define a level-polymorphic $\times$:
1174 \[
1175 \begin{array}{l}
1176 \lcdata{\_\times\_}\ \{a\ b : \lccon{Level}\}\ (A : \lcset{a})\ (B : A \tyarr \lcset{b}) : \lcset{a \sqcup b} \lcwhere \\
1177   \lcind \_ , \_ : (x : A) \tyarr \app{B}{x} \tyarr A \times \app{B}{x}
1178 \end{array}
1179 \]
1180 Levels can be made implicit as shown and can be almost always inferred.
1181 However, having to decorate each type signature with quantified levels adds
1182 quite a lot of noise.  An inference algorithm that automatically quantifies and
1183 instantiates levels (much like Hindley-Milner for types) seems feasible, but is
1184 currently not implemented anywhere.  The ideal situation would be polymorphic,
1185 cumulative levels, with an easy way to omit treatment of levels unless in the
1186 (possibly few) cases where the inference algorithm breaks down.
1187
1188 \subsection{Coinduction}
1189
1190 One thing that the Haskell programmer will miss in ITT as presented is the
1191 possibility to work with infinite data.  The classic example is the manipulation
1192 of infinite streams:
1193 \begin{lstlisting}
1194 zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
1195
1196 fibs :: [Int]
1197 fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
1198 \end{lstlisting}
1199 While we can clearly write useful programs of this kind, we need to be careful,
1200 since \texttt{length fibs}, for example, does not make much sense\footnote{Note
1201   that if instead of machine \texttt{Int}s we used naturals as defined
1202   previously, getting the length of an infinite list would be a productive
1203   definition.}.
1204
1205 In less informal terms, we need to distinguish between \emph{productive} and
1206 non-productive definitions.  A productive definition is one for which pattern
1207 matching on data will never diverge, which is the case for \texttt{fibs} but
1208 not, for example, for \texttt{let x = x in x :: [Int]}.  It is very desirable to
1209 recover \emph{only} the productive definition so that total programs working
1210 with infinite data can be written.
1211
1212 This desire has lead to separate the notion of (finite) data and \emph{codata},
1213 which can be worked on by \emph{coinduction} - an overview is given in
1214 \citep{Jacobs1997}.  Research is very active on this subject since coinduction
1215 as implemented by Coq and Agda is not satisfactory in different ways.
1216
1217 \section{Many equalities}
1218
1219 \subsection{Revision, and function extensionality}
1220 \label{sec:fun-ext}
1221
1222 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1223   schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1224
1225 Up to this point, I have introduced two equalities: \emph{definitional} equality
1226 ($\defeq$) and \emph{propositional} equality ($=_{\tya}$).
1227
1228 Definitional equality relates terms that the type checker identifies as equal.
1229 Our definition in section \ref{sec:core-tt} consisted of `reduce the two terms
1230 to their normal forms, then check if they are equal (up to $\alpha$-renaming)'.
1231 We can extend this definition in other ways so that more terms will be
1232 identified as equal.  Common ones include doing $\eta$-expansion, which converts
1233 partial appications of functions, and congruence laws for $\lambda$:
1234 \begin{center}
1235   \begin{tabular}{c c c}
1236     \AxiomC{$\Gamma \vdash \termf : \tya \tyarr \tyb$}
1237     \UnaryInfC{$\Gamma \vdash \termf \defeq \abs{x}{\app{\termf}{x}}$}
1238     \DisplayProof
1239     &
1240     \AxiomC{$\Gamma; x : \tya \vdash \termf \defeq g$}
1241     \UnaryInfC{$\Gamma \vdash \abs{x}{\termf} \defeq \abs{x}{g}$}
1242     \DisplayProof
1243   \end{tabular}
1244 \end{center}
1245 Definitional equality is used in the type checking process, and this means that
1246 in dependently typed languages type checking and evaluation are intertwined,
1247 since we need to reduce types to normal forms to check if they are equal.  It
1248 also means that we need be able to compute under binders, for example to
1249 determine that $\abs{x}{\app{(\abs{y}{y})}{\tya}} \defeq \abs{x}{\tya}$.  This
1250 process goes on `under the hood' and is outside the control of the user.
1251
1252 Propositional equality, on the other hand, is available to the user to reason
1253 about equality, internalising it as a type.  As we have seen in section
1254 \ref{sec:propeq} propositional equality is introduced by reflexivity and
1255 eliminated with a `Leibnitz's law' style rule ($\lcfun{subst}$).  Note that now
1256 that we have inductive families and dependent pattern matching we do not need
1257 hard coded rules to express this concepts\footnote{Here I use Agda notation, and
1258   thus I cannot redefine $=$ and use subscripts, so I am forced to use $\equiv$
1259   with implicit types.  After I will carry on using the old notation.}:
1260 \[
1261 \begin{array}{l}
1262   \lcdata{\lccon{\_\equiv\_}} : \{A : \lcsetz\} : A \tyarr A \tyarr \lcsetz \lcwhere \\
1263   \begin{array}{l@{\ }l}
1264     \lcind \lccon{refl} &: \{x : A\} \tyarr x \equiv x
1265   \end{array} \\
1266   \\
1267   \lcfun{subst} : \{A : \lcsetz\} \tyarr \{t\ m : A\} \tyarr t \equiv m \tyarr (B : A \tyarr \lcsetz) \tyarr \app{B}{t} \tyarr \app{B}{m} \\
1268   \lcfun{subst} \appsp \lccon{refl}\appsp B \appsp n = n
1269 \end{array}
1270 \]
1271 Here matching $\lccon{refl}$ tells the type checker that $t \defeq m$, and thus
1272 $\app{B}{t} \defeq \app{B}{m}$, so we can just return $n$.  This shows the
1273 connection between type families indices and propositional equality, also
1274 highlighted in \citep{McBride2004}.
1275
1276 It is worth noting that all $\lcfun{subst}$s, in ITT, are guaranteed to reduce
1277 at the top level.  This is because $\lccon{refl}$ is the only constructor for
1278 propositional equality, and thus without false assumptions every top level proof
1279 will have that shape.  Extending this idea to other types, in ITT, at the top
1280 level, expressions have \emph{canonical} values - a property known as
1281 \emph{canonicity}.  We call canonical those values formed by constructors:
1282 $\lambda$, $(,)$, $\lhd$, etc.  In other words a value is canonical if it's not
1283 something that is supposed to reduced (an eliminator) but is stuck on some
1284 variable.
1285
1286 While propositional equality is a very useful construct, we can prove less terms
1287 equal than we would like to.  For example, if we have the usual functions
1288 \[
1289 \begin{array}{l@{\ } l}
1290 \lcfun{id} & : \{A : \lcsetz\} \tyarr A \tyarr A \\
1291 \lcfun{map} & : \{A\ B : \lcsetz\} \tyarr (A \tyarr B) \tyarr \app{\lccon{List}}{A} \tyarr \app{\lccon{List}}{B}
1292 \end{array}
1293 \]
1294 we would certainly like to have a term of type
1295 \[\{A\ B : \lcsetz\} \tyarr
1296 \app{\lcfun{map}}{\lcfun{id}} =_{\lclist{A} \tyarr \lclist{B}} \lcfun{id}\]
1297 We cannot do this in ITT, since the things on the sides of $=$ look too
1298 different at the term level.  What we can have is
1299 \[
1300 \{A\ B : \lcsetz\} \tyarr (x : \lclist{A}) \tyarr \app{\app{\lcfun{map}}{\lcfun{id}}}{x}
1301 =_{\lclist{B}} \app{\lcfun{id}}{x}
1302 \]
1303 Since the type checker has something to compute with (even if only a variable)
1304 and reduce the two lists to equal normal forms.  The root of this problem is
1305 that \emph{function extensionality} does not hold:
1306 \[
1307 \begin{array}{l@{\ } l}
1308 \lcfun{ext} : & \{A\ B : \lcsetz\} \tyarr (f\ g : A \tyarr B) \tyarr \\
1309               & ((x : A) \tyarr \app{f}{x} =_{A} \app{g}{x}) \tyarr f =_{A \tyarr B} g
1310 \end{array}
1311 \]
1312 Which is a long standing issue in ITT.
1313
1314 \subsection{Extensional Type Theory and its problems}
1315
1316 One way to `solve' the problem and gain extensionality is to allow the type
1317 checker to derive definitional equality from propositional equality, introducing
1318 what is known as the `equality reflection' rule:
1319 \begin{center}
1320   \begin{prooftree}
1321     \AxiomC{$\Gamma \vdash t =_A m$}
1322     \UnaryInfC{$\Gamma \vdash t \defeq m$}
1323   \end{prooftree}
1324 \end{center}
1325 This jump from types to a metatheoretic relation has deep consequences.
1326 Firstly, let's get extensionality out of the way.  Given $\Gamma = \Gamma';
1327 \lcfun{eq} : (x : A) \tyarr \app{f}{x} = \app{g}{x}$, we have:
1328 \begin{center}
1329   \begin{prooftree}
1330     \AxiomC{$\Gamma; x : A \vdash \app{\lcfun{eq}}{x} : \app{f}{x} = \app{g}{x}$}
1331     \RightLabel{(equality reflection)}
1332     \UnaryInfC{$\Gamma; x : A \vdash \app{f}{x} \defeq \app{g}{x}$}
1333     \RightLabel{(congruence for $\lambda$)}
1334     \UnaryInfC{$\Gamma \vdash \abs{x : A}{\app{f}{x}} \defeq \abs{x : A}{\app{g}{x}}$}
1335     \RightLabel{($\eta$-law)}
1336     \UnaryInfC{$\Gamma \vdash f \defeq g$}
1337     \RightLabel{($\lccon{refl}$)}
1338     \UnaryInfC{$\Gamma \vdash f = g$}
1339   \end{prooftree}
1340 \end{center}
1341 Since the above is possible, theories that include the equality reflection rule
1342 are often called `Extensional Type Theories', or ETTs.  A notable exponent of
1343 this discipline is the NuPRL system \citep{NuPRL}.  Moreover, equality
1344 reflection simplifies $\lcfun{subst}$-like operations, since if we have $t = m$
1345 and $\app{A}{t}$, then by equality reflection clearly $\app{A}{t} \defeq
1346 \app{A}{m}$.
1347
1348 However equality reflection comes at a great price.  The first hit is that it is
1349 now unsafe to compute under binders, since we can send the type checker into a
1350 loop given that under binders we can have any equality proof we might want
1351 (think about what happens with $\abs{x : \bot}{\dotsb}$), and thus we can
1352 convince the type checker that a term has any type we might want.  Secondly,
1353 equality reflection is not syntax directed, thus the type checker would need to
1354 `invent' proofs of propositional equality to prove two terms definitionally
1355 equal, rendering the type checking process undecidable.  Thus the type checker
1356 needs to carry complete derivations for each typing judgement, instead of
1357 relying on terms only.
1358
1359 \subsection{Observational equality}
1360
1361 \newcommand{\lcprop}{\lccon{Prop}}
1362 \newcommand{\lcdec}[1]{\llbracket #1 \rrbracket}
1363 \newcommand{\lcpropsyn}{\mathit{prop}}
1364 \newcommand{\lcpropf}[3]{\forall #1 : #2.\appsp #3}
1365 \newcommand{\lcparr}{\Rightarrow}
1366
1367 A recent development by \citet{Altenkirch2007} promises to keep the well
1368 behavedness of ITT while being able to gain many useful equality proofs,
1369 including function extensionality.  The main idea is to give the user the
1370 possibility to \emph{coerce} (or transport) values from a type $A$ to a type
1371 $B$, if the type checker can prove structurally that $A$ and $B$ are equal.
1372
1373 This said, starting from a theory similar to the one presented in section
1374 \ref{sec:itt} but with only $\lcset{0}$ and without propositional equality, a
1375 propositional subuniverse of $\lcsetz$ is introduced, plus a `decoding' function
1376 $\lcdec{\_}$:
1377
1378 \begin{center}
1379   \axname{syntax}
1380   $$
1381   \begin{array}{rcl}
1382     \termsyn   & ::= & \dotsb \separ \lcprop \separ \lcdec{\lcpropsyn} \\
1383     \lcpropsyn & ::= & \bot \separ \top \separ \lcpropsyn \wedge \lcpropsyn \separ \lcpropf{x}{\termsyn}{\lcpropsyn}
1384   \end{array}
1385   $$
1386
1387   \axname{typing}
1388
1389   \vspace{0.5cm}
1390
1391   \begin{tabular}{c c c}
1392     \AxiomC{\phantom{1em}}
1393     \UnaryInfC{$\Gamma \vdash \lcprop : \lcsetz$}
1394     \noLine
1395     \UnaryInfC{\phantom{1em}}
1396     \DisplayProof
1397     &
1398     \AxiomC{\phantom{1em}}
1399     \UnaryInfC{$\Gamma \vdash \bot : \lcprop$}
1400     \noLine
1401     \UnaryInfC{$\Gamma \vdash \top : \lcprop$}
1402     \DisplayProof
1403     &
1404     \AxiomC{$\Gamma \vdash P : \lcprop$}
1405     \AxiomC{$\Gamma \vdash Q : \lcprop$}
1406     \BinaryInfC{$\Gamma \vdash P \wedge Q : \lcprop$}
1407     \noLine
1408     \UnaryInfC{\phantom{1em}}
1409     \DisplayProof
1410   \end{tabular}
1411
1412   \vspace{0.5cm}
1413
1414   \begin{tabular}{c c}
1415     \AxiomC{$\Gamma \vdash S : \lcsetz$}
1416     \AxiomC{$\Gamma \vdash Q : \lcprop$}
1417     \BinaryInfC{$\Gamma \vdash \lcpropf{x}{S}{Q} : \lcprop$}
1418     \DisplayProof
1419     &
1420     \AxiomC{$\Gamma \vdash P : \lcprop$}
1421     \UnaryInfC{$\Gamma \vdash \lcdec{P} : \lcsetz$}
1422     \DisplayProof
1423   \end{tabular}
1424
1425   \vspace{0.5cm}
1426
1427   \axdesc{reduction}{\termsyn \bred \termsyn}
1428   \begin{eqnarray*}
1429     \lcdec{\bot}              & \bred & \bot \\
1430     \lcdec{\top}              & \bred & \top \\
1431     \lcdec{P \wedge Q}        & \bred & \lcdec{P} \times \lcdec{Q} \\
1432     \lcdec{\lcpropf{x}{S}{P}} & \bred & (x : S) \tyarr \lcdec{P}
1433   \end{eqnarray*}
1434 \end{center}
1435 I will use $P \lcparr Q$ as an abbreviation for $\lcpropf{\_}{P}{Q}$.  Note that
1436 $\lcprop$ has no `data', but only proofs.  This has the consequence that code
1437 using proofs, when compiled, will be able to safely erase every $\lcprop$, as
1438 long as it doesn't compute under binder - and we most likely don't need to
1439 compute under binders after we type checked and we just need to run the code.
1440 Moreover, we can extend $\lcprop$ with other axioms while retaining canonicity,
1441 since 
1442
1443 \newcommand{\lccoe}[4]{\lcfun{coe}\appsp#1\appsp#2\appsp#3\appsp#4}
1444 \newcommand{\lccoh}[4]{\lcfun{coh}\appsp#1\appsp#2\appsp#3\appsp#4}
1445
1446 Once we have $\lcprop$, we can define the operations that will let us form
1447 equalities and coerce between types and values:
1448 \begin{center}
1449   \axname{typing}
1450
1451   \vspace{0.5cm}
1452
1453   \begin{tabular}{c c}
1454     \AxiomC{$\Gamma \vdash S : \lcsetz$}
1455     \AxiomC{$\Gamma \vdash T : \lcsetz$}
1456     \BinaryInfC{$\Gamma \vdash S = T : \lcprop$}
1457     \DisplayProof
1458     &
1459     \AxiomC{$\Gamma \vdash Q : \lcdec{S = T}$}
1460     \AxiomC{$\Gamma \vdash s : S$}
1461     \BinaryInfC{$\Gamma \vdash \lccoe{S}{T}{Q}{s} : T$}
1462     \DisplayProof
1463   \end{tabular}
1464
1465   \vspace{0.5cm}
1466
1467   \begin{tabular}{c c}
1468     \AxiomC{$\Gamma \vdash s : S$}
1469     \AxiomC{$\Gamma \vdash t : T$}
1470     \BinaryInfC{$\Gamma \vdash (s : S) = (t : T) : \lcprop$}
1471     \DisplayProof
1472     &
1473     \AxiomC{$\Gamma \vdash Q : \lcdec{S = T}$}
1474     \AxiomC{$\Gamma \vdash s : S$}
1475     \BinaryInfC{$\Gamma \vdash \lccoh{S}{T}{Q}{s} : \lcdec{(s : S) = (\lccoe{S}{T}{Q}{s})}$}
1476     \DisplayProof
1477   \end{tabular}
1478 \end{center}
1479
1480 In the first row, $=$ forms equality between types, and $\lcfun{coe}$ (`coerce')
1481 transports values of equal types.  On the second row, $=$ forms equality between
1482 values, and $\lcfun{coh}$ (`coherence') guarantees that all equalities are
1483 really between equal things.  Now the tricky part is to define reduction rules
1484 to reduce the proofs of equality to something $\lcdec{\_}$ can reduce, so that
1485 proof of equality will exist only between equal things, and that $\lcfun{coe}$
1486 will compute only when those proofs are derivable.
1487
1488 Let's start with type-level $=$:
1489 $$
1490 \begin{array}{r@{\ } c@{\ } l c l}
1491   \bot & = & \bot       & \bred & \top \\
1492   \top & = & \top       & \bred & \top \\
1493   \lcbool & = & \lcbool & \bred & \top \\
1494   (s : S) \times T & = & (s' : S') \times T'  & \bred & S = S' \wedge \lcpropf{s}{S}{\lcpropf{s'}{S'}{(s : S) = (s' : S') \lcparr T[x] = T'[x']}} \\
1495   (s : S) \tyarr T & = & (s' : S') \tyarr T'  & \bred & S' = S \wedge \lcpropf{s'}{S'}{\lcpropf{s}{S}{(s' : S') = (s : S) \lcparr T[x] = T'[x']}} \\
1496   \lcw{s}{S}{T} & = & \lcw{s'}{S'}{T'}  & \bred & S = S' \wedge \lcpropf{s}{S}{\lcpropf{s'}{S'}{(s : S) = (s' : S') \lcparr T'[x'] = T[x]}} \\
1497   S & = & T & \bred & \bot\ \text{for every other canonical sets $S$ and $T$}
1498 \end{array}
1499 $$
1500 The rule for $\times$ is unsurprising: it requires the left types to be equal,
1501 and the right types to be equal when the left values are equal.  The rules for
1502 $\tyarr$ and $\lccon{W}$ are similar but with some twists to make the rules for
1503 $\lcfun{coe}$ simpler:
1504 $$
1505 \begin{array}{r@{} l@{} l@{} l c l}
1506   \lccoe{&\bot}{&\bot}{&Q}{z} & \bred & z \\
1507   \lccoe{&\top}{&\top}{&Q}{u} & \bred & u \\
1508   \lccoe{&\lcbool}{&\lcbool}{&Q}{b} & \bred & b \\
1509   \lccoe{&((x : S) \times T)}{&((x' : S') \times T')}{&Q}{(s, t)} & \bred & \\
1510   \multicolumn{6}{l}{
1511       \lcind
1512       \begin{array}{l@{\ } l@{\ } c@{\ } l@{\ }}
1513         \lcsyn{let} & Q_S & \mapsto & \lcfst Q : \lcdec{S = S'} \\
1514                     & s'  & \mapsto & \lccoe{S}{S'}{Q}{s} : S' \\
1515                     & Q_T & \mapsto & \lcsnd Q \appsp s \appsp s' \appsp (\lccoh{S}{S'}{Q_S}{s}) : \lcdec{T[s] = T[s']} \\
1516                     & t'  & \mapsto & \lccoe{T[t]}{T'[s']}{Q_T}{t} : T'[s'] \\
1517         \multicolumn{4}{l}{\lcsyn{in}\ (s', t')}
1518       \end{array}
1519     }\\
1520   \lccoe{&((x : S) \tyarr T)}{&((x' : S') \tyarr T')}{&Q}{f} & \bred & \dotsb \\
1521   \lccoe{&(\lcw{x}{S}{T})}{&(\lcw{x'}{S'}{T'})}{&Q}{(s \lhd f)} & \bred & \dotsb \\
1522   \lccoe{&S}{&T}{&Q}{x} & \bred & \lcabsurdd{T}{Q}
1523 \end{array}
1524 $$
1525 The rule for $\times$ is hairy but straightforward: we are given a $(s, t) : (x
1526 : S) \times T$ and, given the reduction rules specified before, a $$Q : \lcdec{S
1527   = S'} \times ((s : S) \tyarr (s' : S') \tyarr \lcdec{(s : S) = (s' : S')}
1528 \tyarr \lcdec{T[x] = T'[x']})$$ We need to obtain a $(x' : S') \times T'$.  We
1529 can easily get the left part with the left of $Q$ and a coercion, and the right
1530 with the help of $\lcfun{coh}$ and the right of $Q$.  The rules for the other
1531 binders are similar but not reproduced here for brevity.  The reader can refer
1532 to the paper for more details.
1533
1534 Now we are left 
1535
1536 % TODO put the coind paper
1537 \section{What to do}
1538
1539 My goal is to advance the practice of OTT.  Conor McBride and other
1540 collaborators already implemented OTT and other measures as part of efforts
1541 towards a new version of Epigram\footnote{Available at
1542   \url{http://www.e-pig.org/darcs/Pig09/web/}.}.  However the development is
1543 stale right now and it is not clear when and if Epigram 2 will be released.
1544
1545 The first thing that would be very useful to do is to have a small, core
1546 language that supports OTT, on which a more expressive language can be built.
1547 This follows an established tradition in functional programming and theorem
1548 proving of layering several languages of increasing complexity, so that type
1549 checking at a lower level is much simpler and those more prone to bugs.  For
1550 example GHC Haskell uses an internal language, System F\textsubscript{C}
1551 \citep{Sulzmann2007}, which includes only minimal features compared to Haskell.
1552
1553 I have already implemented a type theory as described in section
1554 \ref{sec:itt}\footnote{Available at \url{https://github.com/bitonic/mfixed}.} to
1555 make myself comfortable with how such systems work.  From that, a good starting
1556 point to build something more useful could be $\Pi\Sigma$
1557 \citep{Altenkirch2007}, a core, partial language, thought to be a good target
1558 for high level languages like Agda, with facilities to implement inductive
1559 families and corecursion.  Starting to think about how OTT would work in such a
1560 language is my immediate goal.
1561
1562 If these attempts are successful, I can work towards understanding what a higher
1563 level language would look like and how it would be `elaborated' to the lower
1564 level core theory.  Epigram 2 can certainly be an inspiration, although it
1565 employs a sophisticated reflection system \citep{Chapman2010} that I do not plan
1566 to implement.  In any case, there are many things to do, with the most salients
1567 point being how to treat inductive families and pattern matching, corecursion,
1568 and what a friendly interface for OTT would be from the
1569 programmer/mathematician's perspective (or maybe whether it's feasible to always
1570 infer coercions and/or equality proofs automatically).
1571
1572 Interestingly, the mentioned System F\textsubscript{C} was introduced in GHC to
1573 be able to implement GADTs, which as said in section
1574 \ref{sec:inductive-families} bear many similarities to inductive families.  To
1575 do that it uses a system of coercions that is not too far away from OTT
1576 coercions.  This is not a coincidence, since indices and propositional equality
1577 are often connected, and offers a lot of food for thought.  For instance, GHC
1578 Haskell automatically generates and applies equality proofs, and the inference
1579 engine that serves this purpose is quite complex \citep{Vytiniotis2011} and
1580 often very confusing to the user; we would like to have a less `magic' but
1581 clearer system.
1582
1583 \bibliographystyle{authordate1}
1584 \bibliography{InterimReport}
1585
1586 \appendix
1587 \section{Agda code}
1588 \label{app:agda-code}
1589
1590 \lstset{
1591   xleftmargin=0pt
1592 }
1593
1594 \lstinputlisting{InterimReport.agda}
1595
1596 \end{document}