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