...
[bitonic-mengthesis.git] / thesis.lagda
1 \documentclass[11pt, fleqn]{article}
2 \usepackage{etex}
3
4 \usepackage[sc,slantedGreek]{mathpazo}
5 % \linespread{1.05}
6 % \usepackage{times}
7
8 \oddsidemargin  0in
9 \evensidemargin 0in
10 \textheight 9.5in 
11 \textwidth      6.2in
12 \topmargin      -7mm  
13 %% \parindent   10pt
14
15 \headheight 0pt
16 \headsep 0pt
17
18
19 %% Narrow margins
20 % \usepackage{fullpage}
21
22 %% Bibtex
23 \usepackage{natbib}
24
25 %% Links
26 \usepackage{hyperref}
27
28 %% Frames
29 \usepackage{framed}
30
31 %% Symbols
32 \usepackage[fleqn]{amsmath}
33 \usepackage{stmaryrd}           %llbracket
34
35 %% Proof trees
36 \usepackage{bussproofs}
37
38 %% Diagrams
39 \usepackage[all]{xy}
40
41 %% Quotations
42 \usepackage{epigraph}
43
44 %% Images
45 \usepackage{graphicx}
46
47 %% Subfigure
48 \usepackage{subcaption}
49
50 \usepackage{verbatim}
51 \usepackage{fancyvrb}
52
53 \RecustomVerbatimEnvironment
54   {Verbatim}{Verbatim}
55   {xleftmargin=9mm}
56
57 %% diagrams
58 \usepackage{tikz}
59 \usetikzlibrary{shapes,arrows,positioning}
60 % \usepackage{tikz-cd}
61 % \usepackage{pgfplots}
62
63
64 %% -----------------------------------------------------------------------------
65 %% Commands for Agda
66 \usepackage[english]{babel}
67 \usepackage[conor]{agda}
68 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
69 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
70 \renewcommand{\AgdaField}{\AgdaFunction}
71 % \definecolor{AgdaBound} {HTML}{000000}
72 \definecolor{AgdaHole} {HTML} {FFFF33}
73
74 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
75 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
76 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
77 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
78 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
79 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
80
81 \renewenvironment{code}%
82 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed\small}%
83 {\endpboxed\par\noindent%
84 \ignorespacesafterend\small}
85
86
87 %% -----------------------------------------------------------------------------
88 %% Commands
89
90 \newcommand{\mysmall}{}
91 \newcommand{\mysyn}{\AgdaKeyword}
92 \newcommand{\mytyc}{\AgdaDatatype}
93 \newcommand{\mydc}{\AgdaInductiveConstructor}
94 \newcommand{\myfld}{\AgdaField}
95 \newcommand{\myfun}{\AgdaFunction}
96 \newcommand{\myb}[1]{\AgdaBound{$#1$}}
97 \newcommand{\myfield}{\AgdaField}
98 \newcommand{\myind}{\AgdaIndent}
99 \newcommand{\mykant}{\textsc{Bertus}}
100 \newcommand{\mysynel}[1]{#1}
101 \newcommand{\myse}{\mysynel}
102 \newcommand{\mytmsyn}{\mysynel{term}}
103 \newcommand{\mysp}{\ }
104 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
105 \newcommand{\myappsp}{\hspace{0.07cm}}
106 \newcommand{\myapp}[2]{#1 \myappsp #2}
107 \newcommand{\mysynsep}{\ \ |\ \ }
108 \newcommand{\myITE}[3]{\myfun{If}\, #1\, \myfun{Then}\, #2\, \myfun{Else}\, #3}
109 \newcommand{\mycumul}{\preceq}
110
111 \FrameSep0.2cm
112 \newcommand{\mydesc}[3]{
113   \noindent
114   \mbox{
115     \parbox{\textwidth}{
116       {\mysmall
117         \vspace{0.2cm}
118         \hfill \textbf{#1} $#2$
119         \framebox[\textwidth]{
120           \parbox{\textwidth}{
121             \vspace{0.1cm}
122             \centering{
123               #3
124             }
125             \vspace{0.2cm}
126           }
127         }
128         \vspace{0.2cm}
129       }
130     }
131   }
132 }
133
134 \newcommand{\mytmt}{\mysynel{t}}
135 \newcommand{\mytmm}{\mysynel{m}}
136 \newcommand{\mytmn}{\mysynel{n}}
137 \newcommand{\myred}{\leadsto}
138 \newcommand{\mysub}[3]{#1[#2 / #3]}
139 \newcommand{\mytysyn}{\mysynel{type}}
140 \newcommand{\mybasetys}{K}
141 \newcommand{\mybasety}[1]{B_{#1}}
142 \newcommand{\mytya}{\myse{A}}
143 \newcommand{\mytyb}{\myse{B}}
144 \newcommand{\mytycc}{\myse{C}}
145 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
146 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
147 \newcommand{\myctx}{\Gamma}
148 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
149 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
150 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
151 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
152 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
153 \newcommand{\myunit}{\mytyc{Unit}}
154 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
155 \newcommand{\myfst}{\myfld{fst}}
156 \newcommand{\mysnd}{\myfld{snd}}
157 \newcommand{\myconst}{\myse{c}}
158 \newcommand{\myemptyctx}{\varepsilon}
159 \newcommand{\myhole}{\AgdaHole}
160 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
161 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
162 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
163 \newcommand{\myright}[1]{\mydc{right}_{#1}}
164 \newcommand{\myempty}{\mytyc{Empty}}
165 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
166 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
167 \newcommand{\myarg}{\_}
168 \newcommand{\myderivsp}{}
169 \newcommand{\myderivspp}{\vspace{0.3cm}}
170 \newcommand{\mytyp}{\mytyc{Type}}
171 \newcommand{\myneg}{\myfun{$\neg$}}
172 \newcommand{\myar}{\,}
173 \newcommand{\mybool}{\mytyc{Bool}}
174 \newcommand{\mytrue}{\mydc{true}}
175 \newcommand{\myfalse}{\mydc{false}}
176 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
177 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
178 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
179 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
180 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
181 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
182 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
183 \newcommand{\mylist}{\mytyc{List}}
184 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
185 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
186 \newcommand{\myfoldr}{\myfun{foldr}}
187 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
188 \newcommand{\mynodee}{\mathbin{\mydc{$\lhd$}}}
189 \newcommand{\mynode}[2]{\mynodee_{#1.#2}}
190 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
191 \newcommand{\mylub}{\sqcup}
192 \newcommand{\mydefeq}{\cong}
193 \newcommand{\myrefl}{\mydc{refl}}
194 \newcommand{\mypeq}[1]{\mathrel{\mytyc{=}_{#1}}}
195 \newcommand{\myjeqq}{\myfun{$=$-elim}}
196 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
197 \newcommand{\mysubst}{\myfun{subst}}
198 \newcommand{\myprsyn}{\myse{prop}}
199 \newcommand{\myprdec}[1]{\mathopen{\mytyc{$\llbracket$}} #1 \mathclose{\mytyc{$\rrbracket$}}}
200 \newcommand{\myand}{\mathrel{\mytyc{$\wedge$}}}
201 \newcommand{\mybigand}{\mathrel{\mytyc{$\bigwedge$}}}
202 \newcommand{\myprfora}[3]{\forall #1 {:} #2. #3}
203 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
204 \newcommand{\mybot}{\mytyc{$\bot$}}
205 \newcommand{\mytop}{\mytyc{$\top$}}
206 \newcommand{\mycoe}{\myfun{coe}}
207 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
208 \newcommand{\mycoh}{\myfun{coh}}
209 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
210 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
211 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
212 \newcommand{\myprop}{\mytyc{Prop}}
213 \newcommand{\mytmup}{\mytmsyn\uparrow}
214 \newcommand{\mydefs}{\Delta}
215 \newcommand{\mynf}{\Downarrow}
216 \newcommand{\myinff}[3]{#1 \vdash #2 \Rightarrow #3}
217 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
218 \newcommand{\mychkk}[3]{#1 \vdash #2 \Leftarrow #3}
219 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
220 \newcommand{\myann}[2]{#1 : #2}
221 \newcommand{\mydeclsyn}{\myse{decl}}
222 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
223 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
224 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
225 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ \{ #4 \}}
226 \newcommand{\myelabt}{\vdash}
227 \newcommand{\myelabf}{\rhd}
228 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
229 \newcommand{\mytele}{\Delta}
230 \newcommand{\mytelee}{\delta}
231 \newcommand{\mydcctx}{\Gamma}
232 \newcommand{\mynamesyn}{\myse{name}}
233 \newcommand{\myvec}{\overrightarrow}
234 \newcommand{\mymeta}{\textsc}
235 \newcommand{\myhyps}{\mymeta{hyps}}
236 \newcommand{\mycc}{;}
237 \newcommand{\myemptytele}{\varepsilon}
238 \newcommand{\mymetagoes}{\Longrightarrow}
239 % \newcommand{\mytesctx}{\
240 \newcommand{\mytelesyn}{\myse{telescope}}
241 \newcommand{\myrecs}{\mymeta{recs}}
242 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
243 \newcommand{\mylet}{\mysyn{let}}
244 \newcommand{\myhead}{\mymeta{head}}
245 \newcommand{\mytake}{\mymeta{take}}
246 \newcommand{\myix}{\mymeta{ix}}
247 \newcommand{\myapply}{\mymeta{apply}}
248 \newcommand{\mydataty}{\mymeta{datatype}}
249 \newcommand{\myisreco}{\mymeta{record}}
250 \newcommand{\mydcsep}{\ |\ }
251 \newcommand{\mytree}{\mytyc{Tree}}
252 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
253 \newcommand{\mysigma}{\mytyc{$\Sigma$}}
254 \newcommand{\mynegder}{\vspace{-0.3cm}}
255 \newcommand{\myquot}{\Uparrow}
256 \newcommand{\mynquot}{\, \Downarrow}
257
258 \renewcommand{\[}{\begin{equation*}}
259 \renewcommand{\]}{\end{equation*}}
260 \newcommand{\mymacol}[2]{\text{\textcolor{#1}{$#2$}}}
261
262 %% -----------------------------------------------------------------------------
263
264 \title{\mykant: Implementing Observational Equality}
265 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
266 \date{June 2013}
267
268   \iffalse
269   \begin{code}
270     module thesis where
271   \end{code}
272   \fi
273
274 \begin{document}
275
276 \begin{titlepage}  
277   \centering
278
279   \maketitle
280   \thispagestyle{empty}
281
282   \begin{minipage}{0.4\textwidth}
283   \begin{flushleft} \large
284     \emph{Supervisor:}\\
285     Dr. Steffen \textsc{van Bakel}
286   \end{flushleft}
287 \end{minipage}
288 \begin{minipage}{0.4\textwidth}
289   \begin{flushright} \large
290     \emph{Co-marker:} \\
291     Dr. Philippa \textsc{Gardner}
292   \end{flushright}
293 \end{minipage}
294
295 \end{titlepage}
296
297 \begin{abstract}
298   The marriage between programming and logic has been a very fertile one.  In
299   particular, since the simply typed lambda calculus (STLC), a number of type
300   systems have been devised with increasing expressive power.
301
302   Among this systems, Inutitionistic Type Theory (ITT) has been a very
303   popular framework for theorem provers and programming languages.
304   However, equality has always been a tricky business in ITT and related
305   theories.
306
307   In these thesis we will explain why this is the case, and present
308   Observational Type Theory (OTT), a solution to some of the problems
309   with equality.  We then describe $\mykant$, a theorem prover featuring
310   OTT in a setting more close to the one found in current systems.
311   Having implemented part of $\mykant$ as a Haskell program, we describe
312   some of the implementation issues faced.
313 \end{abstract}
314
315 \clearpage
316
317 \renewcommand{\abstractname}{Acknowledgements}
318 \begin{abstract}
319   I would like to thank Steffen van Backel, my supervisor, who was brave
320   enough to believe in my project and who provided much advice and
321   support.
322
323   I would also like to thank the Haskell and Agda community on
324   \texttt{IRC}, which guided me through the strange world of types; and
325   in particular Andrea Vezzosi and James Deikun, with whom I entertained
326   countless insightful discussions in the past year.  Andrea suggested
327   Observational Type Theory as a topic of study: this thesis would not
328   exist without him.  Before them, Tony Fields introduced me to Haskell,
329   unknowingly filling most of my free time from that time on.
330
331   Finally, much of the work stems from the research of Conor McBride,
332   who answered many of my doubts through these months.  I also owe him
333   the colours.
334 \end{abstract}
335
336 \clearpage
337
338 \tableofcontents
339
340 \clearpage
341
342 \section{Simple and not-so-simple types}
343 \label{sec:types}
344
345 \subsection{The untyped $\lambda$-calculus}
346 \label{sec:untyped}
347
348 Along with Turing's machines, the earliest attempts to formalise computation
349 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
350 language encodes computation with a minimal syntax and no `data' in the
351 traditional sense, but just functions.  Here we give a brief overview of the
352 language, which will give the chance to introduce concepts central to the
353 analysis of all the following calculi.  The exposition follows the one found in
354 chapter 5 of \cite{Queinnec2003}.
355
356 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
357 and applications:
358
359 \mydesc{syntax}{ }{
360   $
361   \begin{array}{r@{\ }c@{\ }l}
362     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
363     x          & \in & \text{Some enumerable set of symbols}
364   \end{array}
365   $
366 }
367
368 Parenthesis will be omitted in the usual way:
369 $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
370 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
371
372 Abstractions roughly corresponds to functions, and their semantics is more
373 formally explained by the $\beta$-reduction rule:
374
375 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
376   $
377   \begin{array}{l}
378     \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{, where} \\
379     \myind{2}
380     \begin{array}{l@{\ }c@{\ }l}
381       \mysub{\myb{x}}{\myb{x}}{\mytmn} & = & \mytmn \\
382       \mysub{\myb{y}}{\myb{x}}{\mytmn} & = & y\text{, with } \myb{x} \neq y \\
383       \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & = & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
384       \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{x}}{\mytmm} \\
385       \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}}, \\
386       \multicolumn{3}{l}{\myind{2} \text{with $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
387     \end{array}
388   \end{array}
389   $
390 }
391
392 The care required during substituting variables for terms is required to avoid
393 name capturing.  We will use substitution in the future for other name-binding
394 constructs assuming similar precautions.
395
396 These few elements are of remarkable expressiveness, and in fact Turing
397 complete.  As a corollary, we must be able to devise a term that reduces forever
398 (`loops' in imperative terms):
399 \[
400   (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
401 \]
402 A \emph{redex} is a term that can be reduced.  In the untyped $\lambda$-calculus
403 this will be the case for an application in which the first term is an
404 abstraction, but in general we call aterm reducible if it appears to the left of
405 a reduction rule.  When a term contains no redexes it's said to be in
406 \emph{normal form}.  Given the observation above, not all terms reduce to a
407 normal forms: we call the ones that do \emph{normalising}, and the ones that
408 don't \emph{non-normalising}.
409
410 The reduction rule presented is not syntax directed, but \emph{evaluation
411   strategies} can be employed to reduce term systematically. Common evaluation
412 strategies include \emph{call by value} (or \emph{strict}), where arguments of
413 abstractions are reduced before being applied to the abstraction; and conversely
414 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
415 to proceed---in other words when we have an application where the function is
416 still not a $\lambda$. In both these reduction strategies we never reduce under
417 an abstraction: for this reason a weaker form of normalisation is used, where
418 both abstractions and normal forms are said to be in \emph{weak head normal
419   form}.
420
421 \subsection{The simply typed $\lambda$-calculus}
422
423 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
424 \emph{types} to them, and then check that the terms that we are forming make
425 sense given our typing rules \citep{Curry1934}.  The first most basic instance
426 of this idea takes the name of \emph{simply typed $\lambda$ calculus}, whose
427 rules are shown in figure \ref{fig:stlc}.
428
429 Our types contain a set of \emph{type variables} $\Phi$, which might
430 correspond to some `primitive' types; and $\myarr$, the type former for
431 `arrow' types, the types of functions.  The language is explicitly
432 typed: when we bring a variable into scope with an abstraction, we
433 declare its type.  Reduction is unchanged from the untyped
434 $\lambda$-calculus.
435
436 \begin{figure}[t]
437   \mydesc{syntax}{ }{
438     $
439     \begin{array}{r@{\ }c@{\ }l}
440       \mytmsyn   & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
441       (\myapp{\mytmsyn}{\mytmsyn}) \\
442       \mytysyn   & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn  \mysynsep \\
443       \myb{x}    & \in & \text{Some enumerable set of symbols} \\
444       \myse{\phi} & \in & \Phi
445     \end{array}
446     $
447   }
448   
449   \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
450       \begin{tabular}{ccc}
451         \AxiomC{$\myctx(x) = A$}
452         \UnaryInfC{$\myjud{\myb{x}}{A}$}
453         \DisplayProof
454         &
455         \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
456         \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
457         \DisplayProof
458         &
459         \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
460         \AxiomC{$\myjud{\mytmn}{\mytya}$}
461         \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
462         \DisplayProof
463       \end{tabular}
464 }
465   \caption{Syntax and typing rules for the STLC.  Reduction is unchanged from
466     the untyped $\lambda$-calculus.}
467   \label{fig:stlc}
468 \end{figure}
469
470 In the typing rules, a context $\myctx$ is used to store the types of bound
471 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
472 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
473
474 This typing system takes the name of `simply typed lambda calculus' (STLC), and
475 enjoys a number of properties.  Two of them are expected in most type systems
476 \citep{Pierce2002}:
477 \begin{description}
478 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
479   constructor does not appear on the left of the $\myred$ relation (currently
480   only $\lambda$), or it can take a step according to the evaluation rules.
481 \item[Preservation] If a well-typed term takes a step of evaluation, then the
482   resulting term is also well-typed, and preserves the previous type.  Also
483   known as \emph{subject reduction}.
484 \end{description}
485
486 However, STLC buys us much more: every well-typed term is normalising
487 \citep{Tait1967}.  It is easy to see that we can't fill the blanks if we want to
488 give types to the non-normalising term shown before:
489 \[
490   \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
491 \]
492
493 This makes the STLC Turing incomplete.  We can recover the ability to loop by
494 adding a combinator that recurses:
495
496 \noindent
497 \begin{minipage}{0.5\textwidth}
498 \mydesc{syntax}{ } {
499   $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
500   \vspace{0.4cm}
501 }
502 \end{minipage} 
503 \begin{minipage}{0.5\textwidth}
504 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
505     \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
506     \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
507     \DisplayProof
508 }
509 \end{minipage} 
510
511 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
512     $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
513 }
514
515 This will deprive us of normalisation, which is a particularly bad thing if we
516 want to use the STLC as described in the next section.
517
518 \subsection{The Curry-Howard correspondence}
519
520 It turns out that the STLC can be seen a natural deduction system for
521 intuitionistic propositional logic.  Terms are proofs, and their types are the
522 propositions they prove.  This remarkable fact is known as the Curry-Howard
523 correspondence, or isomorphism.
524
525 The arrow ($\myarr$) type corresponds to implication.  If we wish to prove that
526 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
527 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
528 correct type:
529 \[
530   \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
531 \]
532 That is, function composition.  Going beyond arrow types, we can extend our bare
533 lambda calculus with useful types to represent other logical constructs, as
534 shown in figure \ref{fig:natded}.
535
536 \begin{figure}[t]
537 \mydesc{syntax}{ }{
538   $
539   \begin{array}{r@{\ }c@{\ }l}
540     \mytmsyn & ::= & \cdots \\
541              &  |  & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
542              &  |  & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
543                      \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
544                      \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
545              &  |  & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
546                      \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
547     \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
548   \end{array}
549   $
550 }
551
552 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
553     \begin{tabular}{cc}
554       $
555       \begin{array}{l@{ }l@{\ }c@{\ }l}
556         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
557           \myapp{\mytmm}{\mytmt} \\
558         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
559           \myapp{\mytmn}{\mytmt}
560       \end{array}
561       $
562       &
563       $
564       \begin{array}{l@{ }l@{\ }c@{\ }l}
565         \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
566         \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
567       \end{array}
568       $
569     \end{tabular}
570 }
571
572 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
573     \begin{tabular}{cc}
574       \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
575       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
576       \DisplayProof
577       &
578       \AxiomC{$\myjud{\mytmt}{\myempty}$}
579       \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
580       \DisplayProof
581     \end{tabular}
582
583   \myderivspp
584
585     \begin{tabular}{cc}
586       \AxiomC{$\myjud{\mytmt}{\mytya}$}
587       \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
588       \DisplayProof
589       &
590       \AxiomC{$\myjud{\mytmt}{\mytyb}$}
591       \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
592       \DisplayProof
593
594     \end{tabular}
595
596   \myderivspp
597
598     \begin{tabular}{cc}
599       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
600       \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
601       \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
602       \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
603       \DisplayProof
604     \end{tabular}
605
606   \myderivspp
607
608     \begin{tabular}{ccc}
609       \AxiomC{$\myjud{\mytmm}{\mytya}$}
610       \AxiomC{$\myjud{\mytmn}{\mytyb}$}
611       \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
612       \DisplayProof
613       &
614       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
615       \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
616       \DisplayProof
617       &
618       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
619       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
620       \DisplayProof
621     \end{tabular}
622 }
623 \caption{Rules for the extendend STLC.  Only the new features are shown, all the
624   rules and syntax for the STLC apply here too.}
625   \label{fig:natded}
626 \end{figure}
627
628 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
629 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
630 products---$\myprod$ here, tuples in Haskell) correspond to
631 conjunctions.  This is apparent looking at the ways to construct and
632 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
633 and $\myright{ }$ correspond to $\vee$ introduction, and
634 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
635 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
636 and $\mysnd$ to $\wedge$ elimination.
637
638 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
639 $\myempty$ corresponds to the logical $\bot$.  $\myunit$ has one introduction
640 rule ($\mytt$), and thus one inhabitant; and no eliminators.  $\myempty$ has no
641 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
642 }$), corresponding to the logical \emph{ex falso quodlibet}.
643
644 With these rules, our STLC now looks remarkably similar in power and use to the
645 natural deduction we already know.  $\myneg \mytya$ can be expressed as $\mytya
646 \myarr \myempty$.  However, there is an important omission: there is no term of
647 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
648 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
649 a type equivalent to those.
650
651 This has a considerable effect on our logic and it's no coincidence, since there
652 is no obvious computational behaviour for laws like the excluded middle.
653 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
654 and all the systems analysed will have this characteristic since they build on
655 the foundation of the STLC.\footnote{There is research to give computational
656   behaviour to classical logic, but I will not touch those subjects.}
657
658 As in logic, if we want to keep our system consistent, we must make sure that no
659 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
660 The variant of STLC presented here is indeed
661 consistent, a result that follows from the fact that it is
662 normalising.
663 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
664 desire for consistency.  The following term works for every type $\mytya$,
665 including bottom:
666 \[(\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya\]
667
668 \subsection{Inductive data}
669 \label{sec:ind-data}
670
671 To make the STLC more useful as a programming language or reasoning tool it is
672 common to include (or let the user define) inductive data types.  These comprise
673 of a type former, various constructors, and an eliminator (or destructor) that
674 serves as primitive recursor.
675
676 For example, we might add a $\mylist$ type constructor, along with an `empty
677 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor.  The eliminator for
678 lists will be the usual folding operation ($\myfoldr$).  See figure
679 \ref{fig:list}.
680
681 \begin{figure}[h]
682 \mydesc{syntax}{ }{
683   $
684   \begin{array}{r@{\ }c@{\ }l}
685     \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
686                      \mysynsep
687                      \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
688     \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
689   \end{array}
690   $
691 }
692 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
693   $
694   \begin{array}{l@{\ }c@{\ }l}
695     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
696
697     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
698     \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
699   \end{array}
700   $
701 }
702 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
703     \begin{tabular}{cc}
704       \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
705       \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
706       \DisplayProof
707       &
708       \AxiomC{$\myjud{\mytmm}{\mytya}$}
709       \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
710       \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
711       \DisplayProof
712     \end{tabular}
713   \myderivspp
714
715     \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
716     \AxiomC{$\myjud{\mytmm}{\mytyb}$}
717     \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
718     \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
719     \DisplayProof
720 }
721 \caption{Rules for lists in the STLC.}
722 \label{fig:list}
723 \end{figure}
724
725 In Section \ref{sec:well-order} we will see how to give a general account of
726 inductive data.
727
728 \section{Intuitionistic Type Theory}
729 \label{sec:itt}
730
731 \subsection{Extending the STLC}
732
733 The STLC can be made more expressive in various ways.  \cite{Barendregt1991}
734 succinctly expressed geometrically how we can add expressivity:
735 $$
736 \xymatrix@!0@=1.5cm{
737   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
738   & & \lambda C \ar@{-}[dd]
739   \\
740   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
741   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
742   \\
743   & \lambda\underline\omega \ar@{-}'[r][rr]
744   & & \lambda P\underline\omega
745   \\
746   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
747   & & \lambda P \ar@{-}[ur]
748 }
749 $$
750 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
751 3 dimensions:
752 \begin{description}
753 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
754   types in our type signatures.  For example, we can define a polymorphic
755   identity function:
756   \[\displaystyle
757   (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} : \mytyp) \myarr \myb{A} \myarr \myb{A}
758   \]
759   The first and most famous instance of this idea has been System F.  This form
760   of polymorphism and has been wildly successful, also thanks to a well known
761   inference algorithm for a restricted version of System F known as
762   Hindley-Milner.  Languages like Haskell and SML are based on this discipline.
763 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
764   type operators.  For example we could define a function that given types $R$
765   and $\mytya$ forms the type that represents a value of type $\mytya$ in
766   continuation passing style:
767   \[\displaystyle(\myabss{\myb{A} \myar \myb{R}}{\mytyp}{(\myb{A}
768     \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp
769   \]
770 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
771   types', give great expressive power.  For example, we can have values of whose
772   type depend on a boolean:
773   \[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
774   \myarr \mytyp\]
775 \end{description}
776
777 All the systems preserve the properties that make the STLC well behaved.  The
778 system we are going to focus on, Intuitionistic Type Theory, has all of the
779 above additions, and thus would sit where $\lambda{C}$ sits in the
780 `$\lambda$-cube'.  It will serve as the logical `core' of all the other
781 extensions that we will present and ultimately our implementation of a similar
782 logic.
783
784 \subsection{A Bit of History}
785
786 Logic frameworks and programming languages based on type theory have a long
787 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
788 but then revised it since the original version was inconsistent due to its
789 impredicativity.\footnote{In the early version there was only one universe
790   $\mytyp$ and $\mytyp : \mytyp$, see Section \ref{sec:term-types} for an
791   explanation on why this causes problems.}  For this reason he gave a revised
792 and consistent definition later \citep{Martin-Lof1984}.
793
794 A related development is the polymorphic $\lambda$-calculus, and specifically
795 the previously mentioned System F, which was developed independently by Girard
796 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
797 fact is that while System F is impredicative it is still consistent and strongly
798 normalising.  \cite{Coquand1986} further extended this line of work with the
799 Calculus of Constructions (CoC).
800
801 Most widely used interactive theorem provers are based on ITT.  Popular ones
802 include Agda \citep{Norell2007, Bove2009}, Coq \citep{Coq}, and Epigram
803 \citep{McBride2004, EpigramTut}.
804
805 \subsection{A simple type theory}
806 \label{sec:core-tt}
807
808 The calculus I present follows the exposition in \citep{Thompson1991},
809 and is quite close to the original formulation of predicative ITT as
810 found in \citep{Martin-Lof1984}.  The system's syntax and reduction
811 rules are presented in their entirety in figure \ref{fig:core-tt-syn}.
812 The typing rules are presented piece by piece.  Agda and \mykant\
813 renditions of the presented theory and all the examples is reproduced in
814 appendix \ref{app:itt-code}.
815
816 \begin{figure}[t]
817 \mydesc{syntax}{ }{
818   $
819   \begin{array}{r@{\ }c@{\ }l}
820     \mytmsyn & ::= & \myb{x} \mysynsep
821                      \mytyp_{level} \mysynsep
822                      \myunit \mysynsep \mytt \mysynsep
823                      \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
824              &  |  & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
825                      \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
826              &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
827                      \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
828                      (\myapp{\mytmsyn}{\mytmsyn}) \\
829              &  |  & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
830                      \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
831              &  |  & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
832              &  |  & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
833                      \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
834              &  |  & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
835     level    & \in & \mathbb{N}
836   \end{array}
837   $
838 }
839
840 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
841     \begin{tabular}{ccc}
842       $
843       \begin{array}{l@{ }l@{\ }c@{\ }l}
844         \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
845         \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
846       \end{array}
847       $
848       &
849       $
850       \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
851       $
852       &
853     $
854     \begin{array}{l@{ }l@{\ }c@{\ }l}
855       \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
856       \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
857     \end{array}
858     $
859     \end{tabular}
860
861     \myderivspp
862
863     $
864     \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
865     \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
866       \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
867     })}
868     $
869 }
870 \caption{Syntax and reduction rules for our type theory.}
871 \label{fig:core-tt-syn}
872 \end{figure}
873
874 \subsubsection{Types are terms, some terms are types}
875 \label{sec:term-types}
876
877 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
878     \begin{tabular}{cc}
879       \AxiomC{$\myjud{\mytmt}{\mytya}$}
880       \AxiomC{$\mytya \mydefeq \mytyb$}
881       \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
882       \DisplayProof
883       &
884       \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
885       \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
886       \DisplayProof
887     \end{tabular}
888 }
889
890 The first thing to notice is that a barrier between values and types that we had
891 in the STLC is gone: values can appear in types, and the two are treated
892 uniformly in the syntax.
893
894 While the usefulness of doing this will become clear soon, a consequence is
895 that since types can be the result of computation, deciding type equality is
896 not immediate as in the STLC.  For this reason we define \emph{definitional
897   equality}, $\mydefeq$, as the congruence relation extending
898 $\myred$---moreover, when comparing types syntactically we do it up to
899 renaming of bound names ($\alpha$-renaming).  For example under this
900 discipline we will find that
901 \[
902 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}}
903 \]
904 Types that are definitionally equal can be used interchangeably.  Here
905 the `conversion' rule is not syntax directed, but it is possible to
906 employ $\myred$ to decide term equality in a systematic way, by always
907 reducing terms to their normal forms before comparing them, so that a
908 separate conversion rule is not needed.
909 Another thing to notice is that considering the need to reduce terms to
910 decide equality, it is essential for a dependently type system to be
911 terminating and confluent for type checking to be decidable.
912
913 Moreover, we specify a \emph{type hierarchy} to talk about `large'
914 types: $\mytyp_0$ will be the type of types inhabited by data:
915 $\mybool$, $\mynat$, $\mylist$, etc.  $\mytyp_1$ will be the type of
916 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
917 \mytyp_0 : \mytyp_1 : \cdots$.  Each type `level' is often called a
918 universe in the literature.  While it is possible to simplify things by
919 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
920 inconsistent for much the same reason that impredicative na\"{\i}ve set
921 theory is \citep{Hurkens1995}.  However various techniques can be
922 employed to lift the burden of explicitly handling universes, as we will
923 see in Section \ref{sec:term-hierarchy}.
924
925 \subsubsection{Contexts}
926
927 \begin{minipage}{0.5\textwidth}
928   \mydesc{context validity:}{\myvalid{\myctx}}{
929       \begin{tabular}{cc}
930         \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
931         \UnaryInfC{$\myvalid{\myemptyctx}$}
932         \DisplayProof
933         &
934         \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
935         \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
936         \DisplayProof
937       \end{tabular}
938   }
939 \end{minipage} 
940 \begin{minipage}{0.5\textwidth}
941   \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
942       \AxiomC{$\myctx(x) = \mytya$}
943       \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
944       \DisplayProof
945   }
946 \end{minipage}
947 \vspace{0.1cm}
948
949 We need to refine the notion context to make sure that every variable appearing
950 is typed correctly, or that in other words each type appearing in the context is
951 indeed a type and not a value.  In every other rule, if no premises are present,
952 we assume the context in the conclusion to be valid.
953
954 Then we can re-introduce the old rule to get the type of a variable for a
955 context.
956
957 \subsubsection{$\myunit$, $\myempty$}
958
959 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
960     \begin{tabular}{ccc}
961       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
962       \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
963       \noLine
964       \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
965       \DisplayProof
966       &
967       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
968       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
969       \noLine
970       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
971       \DisplayProof
972       &
973       \AxiomC{$\myjud{\mytmt}{\myempty}$}
974       \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
975       \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
976       \noLine
977       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
978       \DisplayProof
979     \end{tabular}
980 }
981
982 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
983 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
984 sure that we are invoking $\myabsurd{}$ over a type.
985
986 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
987
988 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
989    \begin{tabular}{ccc}
990      \AxiomC{}
991      \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
992      \DisplayProof
993      &
994      \AxiomC{}
995      \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
996      \DisplayProof
997      &
998      \AxiomC{}
999       \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
1000       \DisplayProof
1001     \end{tabular}
1002     \myderivspp
1003
1004     \AxiomC{$\myjud{\mytmt}{\mybool}$}
1005     \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
1006     \noLine
1007     \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
1008     \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
1009     \DisplayProof
1010 }
1011
1012 With booleans we get the first taste of the `dependent' in `dependent
1013 types'.  While the two introduction rules ($\mytrue$ and $\myfalse$) are
1014 not surprising, the typing rules for $\myfun{if}$ are.  In most strongly
1015 typed languages we expect the branches of an $\myfun{if}$ statements to
1016 be of the same type, to preserve subject reduction, since execution
1017 could take both paths.  This is a pity, since the type system does not
1018 reflect the fact that in each branch we gain knowledge on the term we
1019 are branching on.  Which means that programs along the lines of
1020 \begin{Verbatim}
1021 if null xs then head xs else 0
1022 \end{Verbatim}
1023 are a necessary, well typed, danger.
1024
1025 However, in a more expressive system, we can do better: the branches' type can
1026 depend on the value of the scrutinised boolean.  This is what the typing rule
1027 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1028 representing the scrutinised boolean type, and the branches are typechecked with
1029 the updated knowledge on the value of $\myb{x}$.
1030
1031 \subsubsection{$\myarr$, or dependent function}
1032
1033  \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1034      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1035      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1036      \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1037      \DisplayProof
1038
1039      \myderivspp
1040
1041     \begin{tabular}{cc}
1042       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1043       \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1044       \DisplayProof
1045       &
1046       \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1047       \AxiomC{$\myjud{\mytmn}{\mytya}$}
1048       \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1049       \DisplayProof
1050     \end{tabular}
1051 }
1052
1053 Dependent functions are one of the two key features that perhaps most
1054 characterise dependent types---the other being dependent products.  With
1055 dependent functions, the result type can depend on the value of the
1056 argument.  This feature, together with the fact that the result type
1057 might be a type itself, brings a lot of interesting possibilities.
1058 Following this intuition, in the introduction rule, the return type is
1059 typechecked in a context with an abstracted variable of lhs' type, and
1060 in the elimination rule the actual argument is substituted in the return
1061 type.  Keeping the correspondence with logic alive, dependent functions
1062 are much like universal quantifiers ($\forall$) in logic.
1063
1064 For example, assuming that we have lists and natural numbers in our
1065 language, using dependent functions we would be able to
1066 write:
1067 \[
1068 \begin{array}{l}
1069 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1070 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1071 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1072                \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{$>$}} 0 \myarr
1073                \myb{A}
1074 \end{array}
1075 \]
1076
1077 \myfun{length} is the usual polymorphic length
1078 function. $\myarg\myfun{$>$}\myarg$ is a function that takes two naturals
1079 and returns a type: if the lhs is greater then the rhs, $\myunit$ is
1080 returned, $\myempty$ otherwise.  This way, we can express a
1081 `non-emptyness' condition in $\myfun{head}$, by including a proof that
1082 the length of the list argument is non-zero.  This allows us to rule out
1083 the `empty list' case, so that we can safely return the first element.
1084
1085 Again, we need to make sure that the type hierarchy is respected, which is the
1086 reason why a type formed by $\myarr$ will live in the least upper bound of the
1087 levels of argument and return type.  This trend will continue with the other
1088 type-level binders, $\myprod$ and $\mytyc{W}$.
1089
1090 \subsubsection{$\myprod$, or dependent product}
1091 \label{sec:disju}
1092
1093 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1094      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1095      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1096      \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1097      \DisplayProof
1098
1099      \myderivspp
1100
1101     \begin{tabular}{cc}
1102       \AxiomC{$\myjud{\mytmm}{\mytya}$}
1103       \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1104       \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1105       \noLine
1106       \UnaryInfC{\phantom{$--$}}
1107       \DisplayProof
1108       &
1109       \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1110       \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1111       \noLine
1112       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1113       \DisplayProof
1114     \end{tabular}
1115 }
1116
1117 If dependent functions are a generalisation of $\myarr$ in the STLC,
1118 dependent products are a generalisation of $\myprod$ in the STLC.  The
1119 improvement is that the second element's type can depend on the value of
1120 the first element.  The corrispondence with logic is through the
1121 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1122 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1123 The first element will be a number, and the second evidence that the
1124 number is even.  This highlights the fact that we are working in a
1125 constructive logic: if we have an existence proof, we can always ask for
1126 a witness.  This means, for instance, that $\neg \forall \neg$ is not
1127 equivalent to $\exists$.
1128
1129 Another perhaps more `dependent' application of products, paired with
1130 $\mybool$, is to offer choice between different types.  For example we
1131 can easily recover disjunctions:
1132 \[
1133 \begin{array}{l}
1134   \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1135   \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1136   \myfun{case} : (\myb{A}\ \myb{B}\ \myb{C} {:} \mytyp_0) \myarr (\myb{A} \myarr \myb{C}) \myarr (\myb{B} \myarr \myb{C}) \myarr \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \myarr \myb{C} \\
1137   \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1138   \myind{2} \myapp{(\myitee{\myapp{\myfst}{\myb{x}}}{\myb{b}}{(\myite{\myb{b}}{\myb{A}}{\myb{B}})}{\myb{f}}{\myb{g}})}{(\myapp{\mysnd}{\myb{x}})}
1139 \end{array}
1140 \]
1141
1142 \subsubsection{$\mytyc{W}$, or well-order}
1143 \label{sec:well-order}
1144
1145 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1146   \begin{tabular}{cc}
1147      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1148      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1149      \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1150      \DisplayProof
1151
1152      &
1153
1154      \AxiomC{$\myjud{\mytmt}{\mytya}$}
1155      \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1156      \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1157      \DisplayProof
1158    \end{tabular}
1159
1160      \myderivspp
1161
1162      \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1163      \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1164      \noLine
1165      \BinaryInfC{$\myjud{\myse{p}}{
1166        \myfora{\myb{s}}{\myse{S}}{\myfora{\myb{f}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}} \myarr \myw{\myb{x}}{\myse{S}}{\myse{T}}}{(\myfora{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myb{s}}}{\mysub{\myse{P}}{\myb{w}}{\myapp{\myb{f}}{\myb{t}}}}) \myarr \mysub{\myse{P}}{\myb{w}}{\myb{f}}}}
1167      }$}
1168      \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1169      \DisplayProof
1170 }
1171
1172 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1173 let us represent inductive data in a general (but clumsy) way.  We can
1174 form `nodes' of the shape $\mytmt \mynode{\myb{x}}{\mytyb} \myse{f} :
1175 \myw{\myb{x}}{\mytya}{\mytyb}$ that contain data ($\mytmt$) of type and
1176 one `child' for each member of $\mysub{\mytyb}{\myb{x}}{\mytmt}$.  The
1177 $\myfun{rec}\ \myfun{with}$ acts as an induction principle on
1178 $\mytyc{W}$, given a predicate an a function dealing with the inductive
1179 case---we will gain more intuition about inductive data in ITT in
1180 Section \ref{sec:user-type}.
1181
1182 For example, if we want to form natural numbers, we can take
1183 \[
1184   \begin{array}{@{}l}
1185     \mytyc{Tr} : \mybool \myarr \mytyp_0 \\
1186     \mytyc{Tr} \myappsp \myb{b} \mapsto \myfun{if}\, \myb{b}\, \myunit\, \myfun{else}\, \myempty \\
1187     \ \\
1188     \mynat : \mytyp_0 \\
1189     \mynat \mapsto \myw{\myb{b}}{\mybool}{(\mytyc{Tr}\myappsp\myb{b})}
1190   \end{array}
1191 \]
1192 Each node will contain a boolean.  If $\mytrue$, the number is non-zero,
1193 and we will have one child representing its predecessor, given that
1194 $\mytyc{Tr}$ will return $\myunit$.  If $\myfalse$, the number is zero,
1195 and we will have no predecessors (children), given the $\myempty$:
1196 \[
1197   \begin{array}{@{}l}
1198     \mydc{zero} : \mynat \\
1199     \mydc{zero} \mapsto \myfalse \mynodee (\myabs{\myb{z}}{\myabsurd{\mynat} \myappsp \myb{x}}) \\
1200     \ \\
1201     \mydc{suc} : \mynat \myarr \mynat \\
1202     \mydc{suc}\myappsp \myb{x} \mapsto \mytrue \mynodee (\myabs{\myarg}{\myb{x}})
1203   \end{array}
1204 \]
1205 And with a bit of effort, we can recover addition:
1206 \[
1207   \begin{array}{@{}l}
1208     \myfun{plus} : \mynat \myarr \mynat \myarr \mynat \\
1209     \myfun{plus} \myappsp \myb{x} \myappsp \myb{y} \mapsto \\
1210     \myind{2} \myfun{rec}\, \myb{x} / \myb{b}.\mynat \, \\
1211     \myind{2} \myfun{with}\, \myabs{\myb{b}}{\\
1212       \myind{2}\myind{2}\myfun{if}\, \myb{b} / \myb{b'}.((\mytyc{Tr} \myappsp \myb{b'} \myarr \mynat) \myarr (\mytyc{Tr} \myappsp \myb{b'} \myarr \mynat) \myarr \mynat) \\
1213       \myind{2}\myind{2}\myfun{then}\,(\myabs{\myarg\, \myb{f}}{\mydc{suc}\myappsp (\myapp{\myb{f}}{\mytt})})\, \myfun{else}\, (\myabs{\myarg\, \myarg}{\myb{y}})}
1214   \end{array}
1215   \]
1216 Note how we explicitly have to type the branches to make them
1217 match with the definition of $\mynat$---which gives a taste of the
1218 `clumsiness' of $\mytyc{W}$-types, which while useful as a reasoning
1219 tool are useless to the user modelling data types.
1220
1221 \section{The struggle for equality}
1222 \label{sec:equality}
1223
1224 In the previous section we saw how a type checker (or a human) needs a
1225 notion of \emph{definitional equality}.  Beyond this meta-theoretic
1226 notion, in this section we will explore the ways of expressing equality
1227 \emph{inside} the theory, as a reasoning tool available to the user.
1228 This area is the main concern of this thesis, and in general a very
1229 active research topic, since we do not have a fully satisfactory
1230 solution, yet.  As in the previous section, everything presented is
1231 formalised in Agda in appendix \ref{app:agda-itt}.
1232
1233 \subsection{Propositional equality}
1234
1235 \noindent
1236 \begin{minipage}{0.5\textwidth}
1237 \mydesc{syntax}{ }{
1238   $
1239   \begin{array}{r@{\ }c@{\ }l}
1240     \mytmsyn & ::= & \cdots \\
1241              &  |  & \mytmsyn \mypeq{\mytmsyn} \mytmsyn \mysynsep
1242                      \myapp{\myrefl}{\mytmsyn} \\
1243              &  |  & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1244   \end{array}
1245   $
1246 }
1247 \end{minipage} 
1248 \begin{minipage}{0.5\textwidth}
1249 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1250     $
1251     \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1252     $
1253   \vspace{1.1cm}
1254 }
1255 \end{minipage}
1256 \mynegder
1257 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1258     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1259     \AxiomC{$\myjud{\mytmm}{\mytya}$}
1260     \AxiomC{$\myjud{\mytmn}{\mytya}$}
1261     \TrinaryInfC{$\myjud{\mytmm \mypeq{\mytya} \mytmn}{\mytyp_l}$}
1262     \DisplayProof
1263
1264     \myderivspp
1265
1266     \begin{tabular}{cc}
1267       \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1268       \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mytmm \mypeq{\mytya} \mytmn}$}
1269       \DisplayProof
1270       &
1271       \AxiomC{$
1272         \begin{array}{c}
1273           \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\myb{x} \mypeq{\mytya} \myb{y}}{\mytyp_l}}} \\
1274           \myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1275         \end{array}
1276         $}
1277       \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1278       \DisplayProof
1279     \end{tabular}
1280 }
1281
1282 To express equality between two terms inside ITT, the obvious way to do so is
1283 to have the equality construction to be a type-former.  Here we present what
1284 has survived as the dominating form of equality in systems based on ITT up to
1285 the present day.
1286
1287 Our type former is $\mypeq{\mytya}$, which given a type (in this case
1288 $\mytya$) relates equal terms of that type.  $\mypeq{}$ has one introduction
1289 rule, $\myrefl$, which introduces an equality relation between definitionally
1290 equal terms.
1291
1292 Finally, we have one eliminator for $\mypeq{}$, $\myjeqq$.  $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1293 \begin{itemize}
1294 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1295   $\mytya$) and a proof of their equality
1296 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1297   $\myse{n}$) are equal
1298 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$
1299   twice, plus the trivial proof by reflexivity showing that $\myse{m}$
1300   is equal to itself
1301 \end{itemize}
1302 Given these ingredients, $\myjeqq$ retuns a member of $\myse{P}$ applied to
1303 $\mytmm$, $\mytmn$, and $\myse{q}$.  In other words $\myjeqq$ takes a
1304 witness that $\myse{P}$ works with \emph{definitionally equal} terms, and
1305 returns a witness of $\myse{P}$ working with \emph{propositionally equal}
1306 terms.  Invokations of $\myjeqq$ will vanish when the equality proofs will
1307 reduce to invocations to reflexivity, at which point the arguments must be
1308 definitionally equal, and thus the provided
1309 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1310 can be returned.
1311
1312 While the $\myjeqq$ rule is slightly convoluted, ve can derive many more
1313 `friendly' rules from it, for example a more obvious `substitution' rule, that
1314 replaces equal for equal in predicates:
1315 \[
1316 \begin{array}{l}
1317 \myfun{subst} : \myfora{\myb{A}}{\mytyp}{\myfora{\myb{P}}{\myb{A} \myarr \mytyp}{\myfora{\myb{x}\ \myb{y}}{\myb{A}}{\myb{x} \mypeq{\myb{A}} \myb{y} \myarr \myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{\myb{y}}}}} \\
1318 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1319   \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1320 \end{array}
1321 \]
1322 Once we have $\myfun{subst}$, we can easily prove more familiar laws regarding
1323 equality, such as symmetry, transitivity, congruence laws, etc.
1324
1325 \subsection{Common extensions}
1326
1327 Our definitional and propositional equalities can be enhanced in various
1328 ways.  Obviously if we extend the definitional equality we are also
1329 automatically extend propositional equality, given how $\myrefl$ works.
1330
1331 \subsubsection{$\eta$-expansion}
1332 \label{sec:eta-expand}
1333
1334 A simple extension to our definitional equality is $\eta$-expansion.
1335 Given an abstract variable $\myb{f} : \mytya \myarr \mytyb$ the aim is
1336 to have that $\myb{f} \mydefeq
1337 \myabss{\myb{x}}{\mytya}{\myapp{\myb{f}}{\myb{x}}}$.  We can achieve
1338 this by `expanding' terms based on their types, a process also known as
1339 \emph{quotation}---a term borrowed from the practice of
1340 \emph{normalisation by evaluation}, where we embed terms in some host
1341 language with an existing notion of computation, and then reify them
1342 back into terms, which will `smooth out' differences like the one above
1343 \citep{Abel2007}.
1344
1345 The same concept applies to $\myprod$, where we expand each inhabitant
1346 by reconstructing it by getting its projections, so that $\myb{x}
1347 \mydefeq \mypair{\myfst \myappsp \myb{x}}{\mysnd \myappsp \myb{x}}$.
1348 Similarly, all one inhabitants of $\myunit$ and all zero inhabitants of
1349 $\myempty$ can be considered equal. Quotation can be performed in a
1350 type-directed way, as we will witness in Section \ref{sec:kant-irr}.
1351
1352 To justify this process in our type system we will add a congruence law
1353 for abstractions and a similar law for products, plus the fact that all
1354 elements of $\myunit$ or $\myempty$ are equal.
1355
1356 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1357   \begin{tabular}{cc}
1358     \AxiomC{$\myjudd{\myctx; \myb{y} : \mytya}{\myapp{\myse{f}}{\myb{x}} \mydefeq \myapp{\myse{g}}{\myb{x}}}{\mysub{\mytyb}{\myb{x}}{\myb{y}}}$}
1359     \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1360     \DisplayProof
1361     &
1362     \AxiomC{$\myjud{\mypair{\myapp{\myfst}{\mytmm}}{\myapp{\mysnd}{\mytmm}} \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1363     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1364     \DisplayProof
1365   \end{tabular}
1366
1367   \myderivspp
1368
1369   \begin{tabular}{cc}
1370   \AxiomC{$\myjud{\mytmm}{\myunit}$}
1371   \AxiomC{$\myjud{\mytmn}{\myunit}$}
1372   \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1373   \DisplayProof
1374   &
1375   \AxiomC{$\myjud{\mytmm}{\myempty}$}
1376   \AxiomC{$\myjud{\mytmn}{\myempty}$}
1377   \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myempty}$}
1378   \DisplayProof
1379   \end{tabular}
1380 }
1381
1382 \subsubsection{Uniqueness of identity proofs}
1383
1384 Another common but controversial addition to propositional equality is
1385 the $\myfun{K}$ axiom, which essentially states that all equality proofs
1386 are by reflexivity.
1387
1388 \mydesc{typing:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1389   \AxiomC{$
1390     \begin{array}{@{}c}
1391       \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\myb{x} \mypeq{\mytya} \myb{x} \myarr \mytyp}} \\\
1392       \myjud{\mytmt}{\mytya} \hspace{1cm}
1393       \myjud{\myse{p}}{\myse{P} \myappsp \mytmt \myappsp (\myrefl \myappsp \mytmt)} \hspace{1cm}
1394       \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1395     \end{array}
1396     $}
1397   \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{t} \myappsp \myse{p} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1398   \DisplayProof
1399 }
1400
1401 \cite{Hofmann1994} showed that $\myfun{K}$ is not derivable from the
1402 $\myjeqq$ axiom that we presented, and \cite{McBride2004} showed that it is
1403 needed to implement `dependent pattern matching', as first proposed by
1404 \cite{Coquand1992}.  Thus, $\myfun{K}$ is derivable in the systems that
1405 implement dependent pattern matching, such as Epigram and Agda; but for
1406 example not in Coq.
1407
1408 $\myfun{K}$ is controversial mainly because it is at odds with
1409 equalities that include computational behaviour, most notably
1410 Voevodsky's `Univalent Foundations', which includes a \emph{univalence}
1411 axiom that identifies isomorphisms between types with propositional
1412 equality.  For example we would have two isomorphisms, and thus two
1413 equalities, between $\mybool$ and $\mybool$, corresponding to the two
1414 permutations---one is the identity, and one swaps the elements.  Given
1415 this, $\myfun{K}$ and univalence are inconsistent, and thus a form of
1416 dependent pattern matching that does not imply $\myfun{K}$ is subject of
1417 research.\footnote{More information about univalence can be found at
1418   \url{http://www.math.ias.edu/~vladimir/Site3/Univalent_Foundations.html}.}
1419
1420 \subsection{Limitations}
1421
1422 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1423   schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1424
1425 However, propositional equality as described is quite restricted when
1426 reasoning about equality beyond the term structure, which is what definitional
1427 equality gives us (extension notwithstanding).
1428
1429 The problem is best exemplified by \emph{function extensionality}.  In
1430 mathematics, we would expect to be able to treat functions that give equal
1431 output for equal input as the same.  When reasoning in a mechanised framework
1432 we ought to be able to do the same: in the end, without considering the
1433 operational behaviour, all functions equal extensionally are going to be
1434 replaceable with one another.
1435
1436 However this is not the case, or in other words with the tools we have we have
1437 no term of type
1438 \[
1439 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1440     \myb{A} \myarr \myb{B}}{
1441         (\myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{\myb{B}} \myapp{\myb{g}}{\myb{x}}}) \myarr
1442         \myb{f} \mypeq{\myb{A} \myarr \myb{B}} \myb{g}
1443     }
1444 }
1445 \]
1446 To see why this is the case, consider the functions
1447 \[\myabs{\myb{x}}{0 \mathrel{\myfun{$+$}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 0}\]
1448 where $\myfun{$+$}$ is defined by recursion on the first argument,
1449 gradually destructing it to build up successors of the second argument.
1450 The two functions are clearly extensionally equal, and we can in fact
1451 prove that
1452 \[
1453 \myfora{\myb{x}}{\mynat}{(0 \mathrel{\myfun{$+$}} \myb{x}) \mypeq{\mynat} (\myb{x} \mathrel{\myfun{$+$}} 0)}
1454 \]
1455 By analysis on the $\myb{x}$.  However, the two functions are not
1456 definitionally equal, and thus we won't be able to get rid of the
1457 quantification.
1458
1459 For the reasons above, theories that offer a propositional equality
1460 similar to what we presented are called \emph{intensional}, as opposed
1461 to \emph{extensional}.  Most systems in wide use today (such as Agda,
1462 Coq, and Epigram) are of this kind.
1463
1464 This is quite an annoyance that often makes reasoning awkward to execute.  It
1465 also extends to other fields, for example proving bisimulation between
1466 processes specified by coinduction, or in general proving equivalences based
1467 on the behaviour on a term.
1468
1469 \subsection{Equality reflection}
1470
1471 One way to `solve' this problem is by identifying propositional equality with
1472 definitional equality:
1473
1474 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1475     \AxiomC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}$}
1476     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1477     \DisplayProof
1478 }
1479
1480 This rule takes the name of \emph{equality reflection}, and is a very
1481 different rule from the ones we saw up to now: it links a typing judgement
1482 internal to the type theory to a meta-theoretic judgement that the type
1483 checker uses to work with terms.  It is easy to see the dangerous consequences
1484 that this causes:
1485 \begin{itemize}
1486 \item The rule is syntax directed, and the type checker is presumably expected
1487   to come up with equality proofs when needed.
1488 \item More worryingly, type checking becomes undecidable also because
1489   computing under false assumptions becomes unsafe, since we derive any
1490   equality proof and then use equality reflection and the conversion
1491   rule to have terms of any type.
1492 \end{itemize}
1493
1494 Given these facts theories employing equality reflection, like NuPRL
1495 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1496 to keep the systems manageable.
1497
1498 For all its faults, equality reflection does allow us to prove extensionality,
1499 using the extensions we gave above.  Assuming that $\myctx$ contains
1500 \[\myb{A}, \myb{B} : \mytyp; \myb{f}, \myb{g} : \myb{A} \myarr \myb{B}; \myb{q} : \myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\]
1501 We can then derive
1502 \begin{prooftree}
1503   \mysmall
1504   \AxiomC{$\hspace{1.1cm}\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{q}}{\myb{x}}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\hspace{1.1cm}$}
1505   \RightLabel{equality reflection}
1506   \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1507   \RightLabel{congruence for $\lambda$s}
1508   \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1509   \RightLabel{$\eta$-law for $\lambda$}
1510   \UnaryInfC{$\hspace{1.45cm}\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}\hspace{1.45cm}$}
1511   \RightLabel{$\myrefl$}
1512   \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\myb{f} \mypeq{} \myb{g}}$}
1513 \end{prooftree}
1514
1515 Now, the question is: do we need to give up well-behavedness of our theory to
1516 gain extensionality?
1517
1518 \subsection{Some alternatives}
1519
1520 % TODO finish
1521 % TODO add `extentional axioms' (Hoffman), setoid models (Thorsten)
1522
1523 \section{Observational equality}
1524 \label{sec:ott}
1525
1526 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1527   Theory} (OTT), promises to keep the well behavedness of ITT while
1528 being able to gain many useful equality proofs,\footnote{It is suspected
1529   that OTT gains \emph{all} the equality proofs of ETT, but no proof
1530   exists yet.} including function extensionality.  The main idea is to
1531 give the user the possibility to \emph{coerce} (or transport) values
1532 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1533 structurally that $\mytya$ and $\mytya$ are equal; and providing a
1534 value-level equality based on similar principles.  Here we give an
1535 exposition which follows closely the original paper.
1536
1537 \subsection{A simpler theory, a propositional fragment}
1538
1539 \mydesc{syntax}{ }{
1540     $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1541     $
1542     \begin{array}{r@{\ }c@{\ }l}
1543       \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1544                        \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1545       \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1546       \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1547     \end{array}
1548     $
1549 }
1550
1551 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1552   \begin{tabular}{cc}
1553     \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1554     \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1555     \DisplayProof
1556     &
1557     \AxiomC{$\myjud{\mytmt}{\mybool}$}
1558     \AxiomC{$\myjud{\mytya}{\mytyp}$}
1559     \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1560     \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1561     \DisplayProof
1562   \end{tabular}
1563 }
1564
1565 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1566     \begin{tabular}{ccc}
1567       \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1568       \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1569       \noLine
1570       \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1571       \DisplayProof
1572       &
1573       \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1574       \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1575       \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1576       \noLine
1577       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1578       \DisplayProof
1579       &
1580       \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1581       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1582       \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1583       \noLine
1584       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1585       \DisplayProof
1586     \end{tabular}
1587 }
1588
1589 Our foundation will be a type theory like the one of section
1590 \ref{sec:itt}, with only one level: $\mytyp_0$.  In this context we will
1591 drop the $0$ and call $\mytyp_0$ $\mytyp$.  Moreover, since the old
1592 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1593 types thanks to the hierarchy (which is gone), we need to reintroduce an
1594 ad-hoc conditional for types, where the reduction rule is the obvious
1595 one.
1596
1597 However, we have an addition: a universe of \emph{propositions},
1598 $\myprop$.  $\myprop$ isolates a fragment of types at large, and
1599 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$: \\
1600 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
1601     \begin{tabular}{cc}
1602     $
1603     \begin{array}{l@{\ }c@{\ }l}
1604       \myprdec{\mybot} & \myred & \myempty \\
1605       \myprdec{\mytop} & \myred & \myunit
1606     \end{array}
1607     $
1608     &
1609     $
1610     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
1611       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
1612       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
1613              \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
1614     \end{array}
1615     $
1616     \end{tabular}
1617   } \\
1618   Propositions are what we call the types of \emph{proofs}, or types
1619   whose inhabitants contain no `data', much like $\myunit$.  The goal of
1620   doing this is twofold: erasing all top-level propositions when
1621   compiling; and to identify all equivalent propositions as the same, as
1622   we will see later.
1623
1624   Why did we choose what we have in $\myprop$?  Given the above
1625   criteria, $\mytop$ obviously fits the bill.  A pair of propositions
1626   $\myse{P} \myand \myse{Q}$ still won't get us data. Finally, if
1627   $\myse{P}$ is a proposition and we have
1628   $\myprfora{\myb{x}}{\mytya}{\myse{P}}$ , the decoding will be a
1629   function which returns propositional content.  The only threat is
1630   $\mybot$, by which we can fabricate anything we want: however if we
1631   are consistent there will be nothing of type $\mybot$ at the top
1632   level, which is what we care about regarding proof erasure.
1633
1634 \subsection{Equality proofs}
1635
1636 \mydesc{syntax}{ }{
1637     $
1638     \begin{array}{r@{\ }c@{\ }l}
1639       \mytmsyn & ::= & \cdots \mysynsep
1640       \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
1641       \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1642       \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
1643       \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
1644     \end{array}
1645     $
1646 }
1647
1648 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1649   \begin{tabular}{cc}
1650     \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1651     \AxiomC{$\myjud{\mytmt}{\mytya}$}
1652     \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
1653     \DisplayProof
1654     &
1655   \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1656   \AxiomC{$\myjud{\mytmt}{\mytya}$}
1657   \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
1658   \DisplayProof
1659
1660   \end{tabular}
1661 }
1662
1663 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1664     \begin{tabular}{cc}
1665       \AxiomC{$
1666         \begin{array}{l}
1667           \ \\
1668           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
1669         \end{array}
1670         $}
1671       \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
1672       \DisplayProof
1673       &
1674       \AxiomC{$
1675         \begin{array}{c}
1676           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
1677           \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
1678         \end{array}
1679         $}
1680     \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
1681     \DisplayProof
1682
1683     \end{tabular}
1684 }
1685
1686
1687 While isolating a propositional universe as presented can be a useful
1688 exercises on its own, what we are really after is a useful notion of
1689 equality.  In OTT we want to maintain the notion that things judged to
1690 be equal are still always repleaceable for one another with no
1691 additional changes.  Note that this is not the same as saying that they
1692 are definitionally equal, since as we saw extensionally equal functions,
1693 while satisfying the above requirement, are not definitionally equal.
1694
1695 Towards this goal we introduce two equality constructs in
1696 $\myprop$---the fact that they are in $\myprop$ indicates that they
1697 indeed have no computational content.  The first construct, $\myarg
1698 \myeq \myarg$, relates types, the second,
1699 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values.  The
1700 value-level equality is different from our old propositional equality:
1701 instead of ranging over only one type, we might form equalities between
1702 values of different types---the usefulness of this construct will be
1703 clear soon.  In the literature this equality is known as `heterogeneous'
1704 or `John Major', since
1705
1706 \begin{quote}
1707   John Major's `classless society' widened people's aspirations to
1708   equality, but also the gap between rich and poor. After all, aspiring
1709   to be equal to others than oneself is the politics of envy. In much
1710   the same way, forms equations between members of any type, but they
1711   cannot be treated as equals (ie substituted) unless they are of the
1712   same type. Just as before, each thing is only equal to
1713   itself. \citep{McBride1999}.
1714 \end{quote}
1715
1716 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
1717 transport values between equal types; and $\myfun{coh}$ (`coherence')
1718 guarantees that $\myfun{coe}$ respects the value-level equality, or in
1719 other words that it really has no computational component: if we
1720 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
1721 will still be the same.
1722
1723 Before introducing the core ideas that make OTT work, let us distinguish
1724 between \emph{canonical} and \emph{neutral} types.  Canonical types are
1725 those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
1726 and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$).  Neutral
1727 types are those formed by
1728 $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
1729 Correspondingly, canonical terms are those inhabiting canonical types
1730 ($\mytt$, $\mytrue$, $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$,
1731 ...), and neutral terms those formed by eliminators.\footnote{Using the
1732   terminology from Section \ref{sec:types}, we'd say that canonical
1733   terms are in \emph{weak head normal form}.}  In the current system
1734 (and hopefully in well-behaved systems), all closed terms reduce to a
1735 canonical term, and all canonical types are inhabited by canonical
1736 terms.
1737
1738 \subsubsection{Type equality, and coercions}
1739
1740 The plan is to decompose type-level equalities between canonical types
1741 into decodable propositions containing equalities regarding the
1742 subterms, and to use coerce recursively on the subterms using the
1743 generated equalities.  This interplay between type equalities and
1744 \myfun{coe} ensures that invocations of $\myfun{coe}$ will vanish when
1745 we have evidence of the structural equality of the types we are
1746 transporting terms across.  If the type is neutral, the equality won't
1747 reduce and thus $\myfun{coe}$ won't reduce either.  If we come an
1748 equality between different canonical types, then we reduce the equality
1749 to bottom, making sure that no such proof can exist, and providing an
1750 `escape hatch' in $\myfun{coe}$.
1751
1752 \begin{figure}[t]
1753
1754 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1755     $
1756       \begin{array}{c@{\ }c@{\ }c@{\ }l}
1757         \myempty & \myeq & \myempty & \myred \mytop \\
1758         \myunit  & \myeq &  \myunit & \myred  \mytop \\
1759         \mybool  & \myeq &  \mybool &   \myred  \mytop \\
1760         \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
1761         \multicolumn{4}{l}{
1762           \myind{2} \mytya_1 \myeq \mytyb_1 \myand 
1763                   \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]}
1764                   } \\
1765       \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1766       \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1767       \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1768       \end{array}
1769     $
1770 }
1771 \myderivsp
1772 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
1773   $
1774   \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
1775     \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
1776     \mycoe & \myunit  & \myunit  & \myse{Q} & \myse{t} & \myred & \mytt \\
1777     \mycoe & \mybool  & \mybool  & \myse{Q} & \mytrue & \myred & \mytrue \\
1778     \mycoe & \mybool  & \mybool  & \myse{Q} & \myfalse & \myred & \myfalse \\
1779     \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1780              (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1781              \mytmt_1 & \myred & \\
1782              \multicolumn{7}{l}{
1783              \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
1784                \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
1785                            & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
1786                            & \myb{Q_A}      & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
1787                            & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
1788                            & \myb{Q_B}      & \mapsto & (\myapp{\mysnd}{\myse{Q}}) \myappsp \myb{\mytmm_1} \myappsp \myb{\mytmm_2} \myappsp (\mycohh{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}}) : \myprdec{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \myeq \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}} \\
1789                            & \myb{\mytmn_2} & \mapsto & \mycoee{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}}}{\mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}}{\myb{Q_B}}{\myb{\mytmn_1}} : \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}} \\
1790                \mysyn{in}  & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
1791               \end{array}} \\
1792
1793     \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1794              (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1795              \mytmt & \myred &
1796            \cdots \\
1797
1798     \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1799              (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1800              \mytmt & \myred &
1801            \cdots \\
1802
1803     \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred & \myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1804   \end{array}
1805   $
1806 }
1807 \caption{Reducing type equalities, and using them when
1808   $\myfun{coe}$rcing.}
1809 \label{fig:eqred}
1810 \end{figure}
1811
1812 Figure \ref{fig:eqred} illustrates this idea in practice.  For ground
1813 types, the proof is the trivial element, and \myfun{coe} is the
1814 identity.  For $\myunit$, we can do better: we return its only member
1815 without matching on the term.  For the three type binders, things are
1816 similar but subtly different---the choices we make in the type equality
1817 are dictated by the desire of writing the $\myfun{coe}$ in a natural
1818 way.
1819
1820 $\myprod$ is the easiest case: we decompose the proof into proofs that
1821 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
1822 proof that given equal values in the first element, the types of the
1823 second elements are equal too
1824 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
1825   \myimpl \mytyb_1 \myeq \mytyb_2}$).\footnote{We are using $\myimpl$ to
1826   indicate a $\forall$ where we discard the first value.  We write
1827   $\mytyb_1[\myb{x_1}]$ to indicate that the $\myb{x_1}$ in $\mytyb_1$
1828   is re-bound to the $\myb{x_1}$ quantified by the $\forall$, and
1829   similarly for $\myb{x_2}$ and $\mytyb_2$.}  This also explains the
1830 need for heterogeneous equality, since in the second proof it would be
1831 awkward to express the fact that $\myb{A_1}$ is the same as $\myb{A_2}$.
1832 In the respective $\myfun{coe}$ case, since the types are canonical, we
1833 know at this point that the proof of equality is a pair of the shape
1834 described above.  Thus, we can immediately coerce the first element of
1835 the pair using the first element of the proof, and then instantiate the
1836 second element with the two first elements and a proof by coherence of
1837 their equality, since we know that the types are equal.
1838
1839 The cases for the other binders are omitted for brevity, but they follow
1840 the same principle with some twists to make $\myfun{coe}$ work with the
1841 generated proofs; the reader can refer to the paper for details.
1842
1843 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
1844
1845 It is important to notice that in the reduction rules for $\myfun{coe}$
1846 are never obstructed by the proofs: with the exception of comparisons
1847 between different canonical types we never `pattern match' on the proof
1848 pairs, but always look at the projections.  This means that, as long as
1849 we are consistent, and thus as long as we don't have $\mybot$-inducing
1850 proofs, we can add propositional axioms for equality and $\myfun{coe}$
1851 will still compute.  Thus, we can take $\myfun{coh}$ as axiomatic, and
1852 we can add back familiar useful equality rules:
1853
1854 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1855   \AxiomC{$\myjud{\mytmt}{\mytya}$}
1856   \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mytmt}{\mytya}}}$}
1857   \DisplayProof
1858
1859   \myderivspp
1860
1861   \AxiomC{$\myjud{\mytya}{\mytyp}$}
1862   \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
1863   \BinaryInfC{$\myjud{\mytyc{R} \myappsp (\myb{x} {:} \mytya) \myappsp \mytyb}{\myfora{\myb{y}\, \myb{z}}{\mytya}{\myprdec{\myjm{\myb{y}}{\mytya}{\myb{z}}{\mytya} \myimpl \mysub{\mytyb}{\myb{x}}{\myb{y}} \myeq \mysub{\mytyb}{\myb{x}}{\myb{z}}}}}$}
1864   \DisplayProof
1865 }
1866
1867 $\myrefl$ is the equivalent of the reflexivity rule in propositional
1868 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
1869 abstracting over a value we can substitute equal for equal---this lets
1870 us recover $\myfun{subst}$.  Note that while we need to provide ad-hoc
1871 rules in the restricted, non-hierarchical theory that we have, if our
1872 theory supports abstraction over $\mytyp$s we can easily add these
1873 axioms as abstracted variables.
1874
1875 \subsubsection{Value-level equality}
1876
1877 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1878   $
1879   \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
1880     (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
1881     (&\mytmt_1 & : & \myunit&) & \myeq & (&\mytmt_2 & : & \myunit&) & \myred \mytop \\
1882     (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
1883     (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
1884     (&\mytrue & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mybot \\
1885     (&\myfalse & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mybot \\
1886     (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1887      & \multicolumn{11}{@{}l}{
1888       \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
1889       \myjm{\myapp{\mysnd}{\mytmt_1}}{\mysub{\mytyb_1}{\myb{x_1}}{\myapp{\myfst}{\mytmt_1}}}{\myapp{\mysnd}{\mytmt_2}}{\mysub{\mytyb_2}{\myb{x_2}}{\myapp{\myfst}{\mytmt_2}}}
1890     } \\
1891    (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1892      & \multicolumn{11}{@{}l}{
1893        \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
1894            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
1895            \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
1896          }}
1897     } \\
1898    (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \cdots \\
1899     (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
1900   \end{array}
1901   $
1902 }
1903
1904 As with type-level equality, we want value-level equality to reduce
1905 based on the structure of the compared terms.  When matching
1906 propositional data, such as $\myempty$ and $\myunit$, we automatically
1907 return the trivial type, since if a type has zero one members, all
1908 members will be equal.  When matching on data-bearing types, such as
1909 $\mybool$, we check that such data matches, and return bottom otherwise.
1910
1911 \subsection{Proof irrelevance and stuck coercions}
1912 \label{sec:ott-quot}
1913
1914 The last effort is required to make sure that proofs (members of
1915 $\myprop$) are \emph{irrelevant}.  Since they are devoid of
1916 computational content, we would like to identify all equivalent
1917 propositions as the same, in a similar way as we identified all
1918 $\myempty$ and all $\myunit$ as the same in section
1919 \ref{sec:eta-expand}.
1920
1921 Thus we will have a quotation that will not only perform
1922 $\eta$-expansion, but will also identify and mark proofs that could not
1923 be decoded (that is, equalities on neutral types).  Then, when
1924 comparing terms, marked proofs will be considered equal without
1925 analysing their contents, thus gaining irrelevance.
1926
1927 Moreover we can safely advance `stuck' $\myfun{coe}$rcions between
1928 non-canonical but definitionally equal types.  Consider for example
1929 \[
1930 \mycoee{(\myITE{\myb{b}}{\mynat}{\mybool})}{(\myITE{\myb{b}}{\mynat}{\mybool})}{\myb{x}}
1931 \]
1932 Where $\myb{b}$ and $\myb{x}$ are abstracted variables.  This
1933 $\myfun{coe}$ will not advance, since the types are not canonical.
1934 However they are definitionally equal, and thus we can safely remove the
1935 coerce and return $\myb{x}$ as it is.
1936
1937 This process of identifying every proof as equivalent and removing
1938 $\myfun{coe}$rcions is known as \emph{quotation}.
1939
1940 \section{\mykant : the theory}
1941 \label{sec:kant-theory}
1942
1943 \mykant\ is an interactive theorem prover developed as part of this thesis.
1944 The plan is to present a core language which would be capable of serving as
1945 the basis for a more featureful system, while still presenting interesting
1946 features and more importantly observational equality.
1947
1948 We will first present the features of the system, and then describe the
1949 implementation we have developed in Section \ref{sec:kant-practice}.
1950
1951 The defining features of \mykant\ are:
1952
1953 \begin{description}
1954 \item[Full dependent types] As we would expect, we have dependent a system
1955   which is as expressive as the `best' corner in the lambda cube described in
1956   Section \ref{sec:itt}.
1957
1958 \item[Implicit, cumulative universe hierarchy] The user does not need to
1959   specify universe level explicitly, and universes are \emph{cumulative}.
1960
1961 \item[User defined data types and records] Instead of forcing the user to
1962   choose from a restricted toolbox, we let her define inductive data types,
1963   with associated primitive recursion operators; or records, with associated
1964   projections for each field.
1965
1966 \item[Bidirectional type checking] While no `fancy' inference via
1967   unification is present, we take advantage of a type synthesis system
1968   in the style of \cite{Pierce2000}, extending the concept for user
1969   defined data types.
1970
1971 \item[Observational equality] As described in Section \ref{sec:ott} but
1972   extended to work with the type hierarchy and to admit equality between
1973   arbitrary data types.
1974 \end{description}
1975
1976 We will analyse the features one by one, along with motivations and
1977 tradeoffs for the design decisions made.
1978
1979 \subsection{Bidirectional type checking}
1980
1981 We start by describing bidirectional type checking since it calls for
1982 fairly different typing rules that what we have seen up to now.  The
1983 idea is to have two kinds of terms: terms for which a type can always be
1984 inferred, and terms that need to be checked against a type.  A nice
1985 observation is that this duality runs through the semantics of the
1986 terms: neutral terms (abstracted or defined variables, function
1987 application, record projections, primitive recursors, etc.) \emph{infer}
1988 types, canonical terms (abstractions, record/data types data
1989 constructors, etc.) need to be \emph{checked}.
1990
1991 To introduce the concept and notation, we will revisit the STLC in a
1992 bidirectional style.  The presentation follows \cite{Loh2010}.  The
1993 syntax for our bidirectional STLC is the same as the untyped
1994 $\lambda$-calculus, but with an extra construct to annotate terms
1995 explicitly---this will be necessary when having top-level canonical
1996 terms.  The types are the same as those found in the normal STLC.
1997
1998 \mydesc{syntax}{ }{
1999   $
2000   \begin{array}{r@{\ }c@{\ }l}
2001     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep (\mytmsyn : \mytysyn)
2002   \end{array}
2003   $
2004 }
2005 We will have two kinds of typing judgements: \emph{inference} and
2006 \emph{checking}.  $\myinf{\mytmt}{\mytya}$ indicates that $\mytmt$
2007 infers the type $\mytya$, while $\mychk{\mytmt}{\mytya}$ can be checked
2008 against type $\mytya$.  The type of variables in context is inferred,
2009 and so are annotate terms.  The type of applications is inferred too,
2010 propagating types down the applied term.  Abstractions are checked.
2011 Finally, we have a rule to check the type of an inferrable term.
2012
2013 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
2014   \begin{tabular}{ccc}
2015     \AxiomC{$\myctx(x) = A$}
2016     \UnaryInfC{$\myinf{\myb{x}}{A}$}
2017     \DisplayProof
2018     &
2019     \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
2020     \UnaryInfC{$\mychk{\myabs{x}{\mytmt}}{\mytyb}$}
2021     \DisplayProof
2022     &
2023     \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
2024     \AxiomC{$\myjud{\mytmn}{\mytya}$}
2025     \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
2026     \DisplayProof
2027   \end{tabular}
2028
2029   \myderivspp
2030
2031   \begin{tabular}{cc}
2032     \AxiomC{$\mychk{\mytmt}{\mytya}$}
2033     \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2034     \DisplayProof
2035     &
2036     \AxiomC{$\myinf{\mytmt}{\mytya}$}
2037     \UnaryInfC{$\mychk{\mytmt}{\mytya}$}
2038     \DisplayProof
2039   \end{tabular}
2040 }
2041
2042 For example, if we wanted to type function composition (in this case for
2043 naturals), we would have to annotate the term:
2044 \[
2045   \myfun{comp} \mapsto (\myabs{\myb{f}\, \myb{g}\, \myb{x}}{\myb{f}\myappsp(\myb{g}\myappsp\myb{x})}) : (\mynat \myarr \mynat) \myarr (\mynat \myarr \mynat) \myarr \mynat \myarr \mynat
2046 \]
2047 But we would not have to annotate functions passed to it, since the type would be propagated to the arguments:
2048 \[
2049    \myfun{comp}\myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 3}) \myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$*$}} 4}) \myappsp 42
2050 \]
2051
2052 \subsection{Base terms and types}
2053
2054 Let us begin by describing the primitives available without the user
2055 defining any data types, and without equality.  The way we handle
2056 variables and substitution is left unspecified, and explained in section
2057 \ref{sec:term-repr}, along with other implementation issues.  We are
2058 also going to give an account of the implicit type hierarchy separately
2059 in Section \ref{sec:term-hierarchy}, so as not to clutter derivation
2060 rules too much, and just treat types as impredicative for the time
2061 being.
2062
2063 \mydesc{syntax}{ }{
2064   $
2065   \begin{array}{r@{\ }c@{\ }l}
2066     \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
2067     &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
2068     \myabs{\myb{x}}{\mytmsyn} \mysynsep
2069     (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
2070     (\myann{\mytmsyn}{\mytmsyn}) \\
2071     \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
2072   \end{array}
2073   $
2074 }
2075
2076 The syntax for our calculus includes just two basic constructs:
2077 abstractions and $\mytyp$s.  Everything else will be provided by
2078 user-definable constructs.  Since we let the user define values, we will
2079 need a context capable of carrying the body of variables along with
2080 their type.
2081
2082 Bound names and defined names are treated separately in the syntax, and
2083 while both can be associated to a type in the context, only defined
2084 names can be associated with a body:
2085
2086 \mydesc{context validity:}{\myvalid{\myctx}}{
2087     \begin{tabular}{ccc}
2088       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
2089       \UnaryInfC{$\myvalid{\myemptyctx}$}
2090       \DisplayProof
2091       &
2092       \AxiomC{$\myjud{\mytya}{\mytyp}$}
2093       \AxiomC{$\mynamesyn \not\in \myctx$}
2094       \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
2095       \DisplayProof
2096       &
2097       \AxiomC{$\myjud{\mytmt}{\mytya}$}
2098       \AxiomC{$\myfun{f} \not\in \myctx$}
2099       \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
2100       \DisplayProof
2101     \end{tabular}
2102 }
2103
2104 Now we can present the reduction rules, which are unsurprising.  We have
2105 the usual function application ($\beta$-reduction), but also a rule to
2106 replace names with their bodies ($\delta$-reduction), and one to discard
2107 type annotations.  For this reason reduction is done in-context, as
2108 opposed to what we have seen in the past:
2109
2110 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
2111     \begin{tabular}{ccc}
2112       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2113       \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
2114                   \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
2115       \DisplayProof
2116       &
2117       \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
2118       \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
2119       \DisplayProof
2120       &
2121       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2122       \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
2123       \DisplayProof
2124     \end{tabular}
2125 }
2126
2127 We can now give types to our terms.  Although we include the usual
2128 conversion rule, we defer a detailed account of definitional equality to
2129 Section \ref{sec:kant-irr}.
2130
2131 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{   
2132     \begin{tabular}{cccc}
2133       \AxiomC{$\myse{name} : A \in \myctx$}
2134       \UnaryInfC{$\myinf{\myse{name}}{A}$}
2135       \DisplayProof
2136       &
2137       \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
2138       \UnaryInfC{$\myinf{\myfun{f}}{A}$}
2139       \DisplayProof
2140       &
2141       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2142       \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2143       \DisplayProof
2144       &
2145       \AxiomC{$\myinf{\mytmt}{\mytya}$}
2146       \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
2147       \BinaryInfC{$\mychk{\mytmt}{\mytyb}$}
2148       \DisplayProof
2149     \end{tabular}
2150     \myderivspp
2151
2152     \begin{tabular}{ccc}
2153       \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
2154       \AxiomC{$\mychk{\mytmn}{\mytya}$}
2155       \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
2156       \DisplayProof
2157
2158       &
2159
2160       \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
2161       \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2162       \DisplayProof
2163     \end{tabular}
2164 }
2165
2166 \subsection{Elaboration}
2167
2168 As we mentioned, $\mykant$\ allows the user to define not only values
2169 but also custom data types and records.  \emph{Elaboration} consists of
2170 turning these declarations into workable syntax, types, and reduction
2171 rules.  The treatment of custom types in $\mykant$\ is heavily inspired
2172 by McBride and McKinna early work on Epigram \citep{McBride2004},
2173 although with some differences.
2174
2175 \subsubsection{Term vectors, telescopes, and assorted notation}
2176
2177 We use a vector notation to refer to a series of term applied to
2178 another, for example $\mytyc{D} \myappsp \vec{A}$ is a shorthand for
2179 $\mytyc{D} \myappsp \mytya_1 \cdots \mytya_n$, for some $n$.  $n$ is
2180 consistently used to refer to the length of such vectors, and $i$ to
2181 refer to an index in such vectors.  We also often need to `build up'
2182 terms vectors, in which case we use $\myemptyctx$ for an empty vector
2183 and add elements to an existing vector with $\myarg ; \myarg$, similarly
2184 to what we do for context.
2185
2186 To present the elaboration and operations on user defined data types, we
2187 frequently make use what de Bruijn called \emph{telescopes}
2188 \citep{Bruijn91}, a construct that will prove useful when dealing with
2189 the types of type and data constructors.  A telescope is a series of
2190 nested typed bindings, such as $(\myb{x} {:} \mynat); (\myb{p} {:}
2191 \myapp{\myfun{even}}{\myb{x}})$.  Consistently with the notation for
2192 contexts and term vectors, we use $\myemptyctx$ to denote an empty
2193 telescope and $\myarg ; \myarg$ to add a new binding to an existing
2194 telescope.
2195
2196 We refer to telescopes with $\mytele$, $\mytele'$, $\mytele_i$, etc.  If
2197 $\mytele$ refers to a telescope, $\mytelee$ refers to the term vector
2198 made up of all the variables bound by $\mytele$.  $\mytele \myarr
2199 \mytya$ refers to the type made by turning the telescope into a series
2200 of $\myarr$.  Returning to the examples above, we have that
2201 \[
2202    (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2203    (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2204 \]
2205
2206 We make use of various operations to manipulate telescopes:
2207 \begin{itemize}
2208 \item $\myhead(\mytele)$ refers to the first type appearing in
2209   $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2210   \myapp{\myfun{even}}{\myb{x}})) = \mynat$.  Similarly,
2211   $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2212   (1-indexed).
2213 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2214   first $i$ elements of $\mytele$:  $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2215   \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$.
2216 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2217   terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2218   \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2219   \myapp{\myfun{even}}{42})$.
2220 \end{itemize}
2221
2222 Additionally, when presenting syntax elaboration, I'll use $\mytmsyn^n$
2223 to indicate a term vector composed of $n$ elements, or
2224 $\mytmsyn^{\mytele}$ for one composed by as many elements as the
2225 telescope.
2226
2227 \subsubsection{Declarations syntax}
2228
2229 \mydesc{syntax}{ }{
2230   $
2231   \begin{array}{r@{\ }c@{\ }l}
2232       \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2233                  &  |  & \mypost{\myb{x}}{\mytmsyn} \\
2234                  &  |  & \myadt{\mytyc{D}}{\myappsp \mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2235                  &  |  & \myreco{\mytyc{D}}{\myappsp \mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2236
2237       \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2238       \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2239   \end{array}
2240   $
2241 }
2242
2243 In \mykant\ we have four kind of declarations:
2244
2245 \begin{description}
2246 \item[Defined value] A variable, together with a type and a body.
2247 \item[Abstract variable] An abstract variable, with a type but no body.
2248 \item[Inductive data] A datatype, with a type constructor and various data
2249   constructors---somewhat similar to what we find in Haskell.  A primitive
2250   recursor (or `destructor') will be generated automatically.
2251 \item[Record] A record, which consists of one data constructor and various
2252   fields, with no recursive occurrences.
2253 \end{description}
2254
2255 Elaborating defined variables consists of type checking body against the
2256 given type, and updating the context to contain the new binding.
2257 Elaborating abstract variables and abstract variables consists of type
2258 checking the type, and updating the context with a new typed variable:
2259
2260 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2261     \begin{tabular}{cc}
2262       \AxiomC{$\myjud{\mytmt}{\mytya}$}
2263       \AxiomC{$\myfun{f} \not\in \myctx$}
2264       \BinaryInfC{
2265         $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \  \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2266       }
2267       \DisplayProof
2268       &
2269       \AxiomC{$\myjud{\mytya}{\mytyp}$}
2270       \AxiomC{$\myfun{f} \not\in \myctx$}
2271       \BinaryInfC{
2272         $
2273           \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2274           \ \ \myelabf\ \  \myctx; \myfun{f} : \mytya
2275         $
2276       }
2277       \DisplayProof
2278     \end{tabular}
2279 }
2280
2281 \subsubsection{User defined types}
2282 \label{sec:user-type}
2283
2284 Elaborating user defined types is the real effort.  First, let's explain
2285 what we can defined, with some examples.
2286
2287 \begin{description}
2288 \item[Natural numbers] To define natural numbers, we create a data type
2289   with two constructors: one with zero arguments ($\mydc{zero}$) and one
2290   with one recursive argument ($\mydc{suc}$):
2291   \[
2292   \begin{array}{@{}l}
2293     \myadt{\mynat}{ }{ }{
2294       \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2295     }
2296   \end{array}
2297   \]
2298   This is very similar to what we would write in Haskell:
2299   \begin{Verbatim}
2300 data Nat = Zero | Suc Nat
2301   \end{Verbatim}
2302   Once the data type is defined, $\mykant$\ will generate syntactic
2303   constructs for the type and data constructors, so that we will have
2304   \begin{center}
2305     \mysmall
2306     \begin{tabular}{ccc}
2307       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2308       \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2309       \DisplayProof
2310     &
2311       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2312       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2313       \DisplayProof
2314     &
2315       \AxiomC{$\mychk{\mytmt}{\mynat}$}
2316       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2317       \DisplayProof
2318     \end{tabular}
2319   \end{center}
2320   While in Haskell (or indeed in Agda or Coq) data constructors are
2321   treated the same way as functions, in $\mykant$\ they are syntax, so
2322   for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will be a
2323   syntax error.  This is necessary so that we can easily infer the type
2324   of polymorphic data constructors, as we will see later.
2325
2326   Moreover, each data constructor is prefixed by the type constructor
2327   name, since we need to retrieve the type constructor of a data
2328   constructor when type checking.  This measure aids in the presentation
2329   of various features but it is not needed in the implementation, where
2330   we can have a dictionary to lookup the type constructor corresponding
2331   to each data constructor.  When using data constructors in examples I
2332   will omit the type constructor prefix for brevity.
2333
2334   Along with user defined constructors, $\mykant$\ automatically
2335   generates an \emph{eliminator}, or \emph{destructor}, to compute with
2336   natural numbers: If we have $\mytmt : \mynat$, we can destruct
2337   $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2338   \begin{prooftree}
2339     \mysmall
2340     \AxiomC{$\mychk{\mytmt}{\mynat}$}
2341     \UnaryInfC{$
2342       \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2343         \begin{array}{@{}l}
2344           \myfora{\myb{P}}{\mynat \myarr \mytyp}{ \\ \myapp{\myb{P}}{\mydc{zero}} \myarr (\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}) \myarr \\ \myapp{\myb{P}}{\mytmt}}
2345           \end{array}
2346         }$}
2347   \end{prooftree}
2348   $\mynat.\myfun{elim}$ corresponds to the induction principle for
2349   natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2350   know that predicate holds for the base case
2351   ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2352   ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2353     \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2354   holds for any number.  As with the data constructors, we require the
2355   eliminator to be applied to the `destructed' element.
2356
2357   While the induction principle is usually seen as a mean to prove
2358   properties about numbers, in the intuitionistic setting it is also a
2359   mean to compute.  In this specific case we will $\mynat.\myfun{elim}$
2360   will return the base case if the provided number is $\mydc{zero}$, and
2361   recursively apply the inductive step if the number is a
2362   $\mydc{suc}$cessor:
2363   \[
2364   \begin{array}{@{}l@{}l}
2365     \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2366     \mytyc{\mynat}.\myfun{elim} \myappsp (\mydc{suc} \myappsp \mytmt) & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{ps} \myappsp \mytmt \myappsp (\mynat.\myfun{elim} \myappsp \mytmt \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps})
2367   \end{array}
2368   \]
2369   The Haskell equivalent would be
2370   \begin{Verbatim}
2371 elim :: Nat -> a -> (Nat -> a -> a) -> a
2372 elim Zero    pz ps = pz
2373 elim (Suc n) pz ps = ps n (elim n pz ps)
2374 \end{Verbatim}
2375   Which buys us the computational behaviour, but not the reasoning power.
2376
2377 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2378   lists are too similar to natural numbers to be interesting.
2379   \[
2380   \begin{array}{@{}l}
2381     \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2382       \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2383     }
2384   \end{array}
2385   \]
2386   Now the purpose of constructors as syntax can be explained: what would
2387   the type of $\mydc{leaf}$ be?  If we were to treat it as a `normal'
2388   term, we would have to specify the type parameter of the tree each
2389   time the constructor is applied:
2390   \[
2391   \begin{array}{@{}l@{\ }l}
2392     \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2393     \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2394   \end{array}
2395   \]
2396   The problem with this approach is that creating terms is incredibly
2397   verbose and dull, since we would need to specify the type parameters
2398   each time.  For example if we wished to create a $\mytree \myappsp
2399   \mynat$ with two nodes and three leaves, we would have to write
2400   \[
2401   \mydc{node} \myappsp \mynat \myappsp (\mydc{node} \myappsp \mynat \myappsp (\mydc{leaf} \myappsp \mynat) \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp (\mydc{leaf} \myappsp \mynat)) \myappsp \mydc{zero} \myappsp (\mydc{leaf} \myappsp \mynat)
2402   \]
2403   The redundancy of $\mynat$s is quite irritating.  Instead, if we treat
2404   constructors as syntactic elements, we can `extract' the type of the
2405   parameter from the type that the term gets checked against, much like
2406   we get the type of abstraction arguments:
2407   \begin{center}
2408     \mysmall
2409     \begin{tabular}{cc}
2410       \AxiomC{$\mychk{\mytya}{\mytyp}$}
2411       \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2412       \DisplayProof
2413       &
2414       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2415       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2416       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2417       \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2418       \DisplayProof
2419     \end{tabular}
2420   \end{center}
2421   Which enables us to write, much more concisely
2422   \[
2423   \mydc{node} \myappsp (\mydc{node} \myappsp \mydc{leaf} \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp \mydc{leaf}) \myappsp \mydc{zero} \myappsp \mydc{leaf} : \myapp{\mytree}{\mynat}
2424   \]
2425   We gain an annotation, but we lose the myriad of types applied to the
2426   constructors.  Conversely, with the eliminator for $\mytree$, we can
2427   infer the type of the arguments given the type of the destructed:
2428   \begin{prooftree}
2429     \small
2430     \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2431     \UnaryInfC{$
2432       \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2433         \begin{array}{@{}l}
2434           (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2435           \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2436           ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2437           \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2438           \myapp{\myb{P}}{\mytmt}
2439         \end{array}
2440       }
2441       $}
2442   \end{prooftree}
2443   As expected, the eliminator embodies structural induction on trees.
2444
2445 \item[Empty type] We have presented types that have at least one
2446   constructors, but nothing prevents us from defining types with
2447   \emph{no} constructors:
2448   \[\myadt{\mytyc{Empty}}{ }{ }{ }\]
2449   What shall the `induction principle' on $\mytyc{Empty}$ be?  Does it
2450   even make sense to talk about induction on $\mytyc{Empty}$?
2451   $\mykant$\ does not care, and generates an eliminator with no `cases',
2452   and thus corresponding to the $\myfun{absurd}$ that we know and love:
2453   \begin{prooftree}
2454     \mysmall
2455     \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2456     \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2457   \end{prooftree}
2458
2459 \item[Ordered lists] Up to this point, the examples shown are nothing
2460   new to the \{Haskell, SML, OCaml, functional\} programmer.  However
2461   dependent types let us express much more than that.  A useful example
2462   is the type of ordered lists. There are many ways to define such a
2463   thing, we will define our type to store the bounds of the list, making
2464   sure that $\mydc{cons}$ing respects that.
2465
2466   First, using $\myunit$ and $\myempty$, we define a type expressing the
2467   ordering on natural numbers, $\myfun{le}$---`less or equal'.
2468   $\myfun{le}\myappsp \mytmm \myappsp \mytmn$ will be inhabited only if
2469   $\mytmm \le \mytmn$:
2470   \[
2471     \begin{array}{@{}l}
2472       \myfun{le} : \mynat \myarr \mynat \myarr \mytyp \\
2473       \myfun{le} \myappsp \myb{n} \mapsto \\
2474           \myind{2} \mynat.\myfun{elim} \\
2475             \myind{2}\myind{2} \myb{n} \\
2476             \myind{2}\myind{2} (\myabs{\myarg}{\mynat \myarr \mytyp}) \\
2477             \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2478             \myind{2}\myind{2} (\myabs{\myb{n}\, \myb{f}\, \myb{m}}{
2479               \mynat.\myfun{elim} \myappsp \myb{m} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myb{m'}\, \myarg}{\myapp{\myb{f}}{\myb{m'}}})
2480                               })
2481     \end{array}
2482     \]
2483   We return $\myunit$ if the scrutinised is $\mydc{zero}$ (every
2484   number in less or equal than zero), $\myempty$ if the first number is
2485   a $\mydc{suc}$cessor and the second a $\mydc{zero}$, and we recurse if
2486   they are both successors.  Since we want the list to have possibly
2487   `open' bounds, for example for empty lists, we create a type for
2488   `lifted' naturals with a bottom (less than everything) and top
2489   (greater than everything) elements, along with an associated comparison
2490   function:
2491   \[
2492     \begin{array}{@{}l}
2493     \myadt{\mytyc{Lift}}{ }{ }{\mydc{bot} \mydcsep \mydc{lift} \myappsp \mynat \mydcsep \mydc{top}}\\
2494     \myfun{le'} : \mytyc{Lift} \myarr \mytyc{Lift} \myarr \mytyp\\
2495     \myfun{le'} \myappsp \myb{l_1} \mapsto \\
2496           \myind{2} \mytyc{Lift}.\myfun{elim} \\
2497             \myind{2}\myind{2} \myb{l_1} \\
2498             \myind{2}\myind{2} (\myabs{\myarg}{\mytyc{Lift} \myarr \mytyp}) \\
2499             \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2500             \myind{2}\myind{2} (\myabs{\myb{n_1}\, \myb{n_2}}{
2501               \mytyc{Lift}.\myfun{elim} \myappsp \myb{l_2} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myb{n_2}}{\myfun{le} \myappsp \myb{n_1} \myappsp \myb{n_2}}) \myappsp \myunit
2502             }) \\
2503             \myind{2}\myind{2} (\myabs{\myb{n_1}\, \myb{n_2}}{
2504               \mytyc{Lift}.\myfun{elim} \myappsp \myb{l_2} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myarg}{\myempty}) \myappsp \myunit
2505             })
2506     \end{array}
2507     \]
2508   Finally, we can defined a type of ordered lists.  The type is
2509   parametrised over two values representing the lower and upper bounds
2510   of the elements, as opposed to the type parameters that we are used
2511   to.  Then, an empty list will have to have evidence that the bounds
2512   are ordered, and each time we add an element we require the list to
2513   have a matching lower bound:
2514   \[
2515     \begin{array}{@{}l}
2516       \myadt{\mytyc{OList}}{\myappsp (\myb{low}\ \myb{upp} {:} \mytyc{Lift})}{\\ \myind{2}}{
2517           \mydc{nil} \myappsp (\myfun{le'} \myappsp \myb{low} \myappsp \myb{upp}) \mydcsep \mydc{cons} \myappsp (\myb{n} {:} \mynat) \myappsp (\mytyc{OList} \myappsp (\myfun{lift} \myappsp \myb{n}) \myappsp \myb{upp}) \myappsp (\myfun{le'} \myappsp \myb{low} \myappsp (\myfun{lift} \myappsp \myb{n})
2518         }
2519     \end{array}
2520   \]
2521   If we want we can then employ this structure to write and prove
2522   correct various sorting algorithms.\footnote{See this presentation by
2523     Conor McBride:
2524     \url{https://personal.cis.strath.ac.uk/conor.mcbride/Pivotal.pdf},
2525     and this blog post by the author:
2526     \url{http://mazzo.li/posts/AgdaSort.html}.}
2527
2528 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
2529   us another way to define types: $\mysyn{record}$.  A record is a
2530   datatype with one constructor and `projections' to extract specific
2531   fields of the said constructor.
2532
2533   For example, we can recover dependent products:
2534   \[
2535   \begin{array}{@{}l}
2536     \myreco{\mytyc{Prod}}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
2537   \end{array}
2538   \]
2539   Here $\myfst$ and $\mysnd$ are the projections, with their respective
2540   types.  Note that each field can refer to the preceding fields.  A
2541   constructor will be automatically generated, under the name of
2542   $\mytyc{Prod}.\mydc{constr}$.  Dually to data types, we will omit the
2543   type constructor prefix for record projections.
2544
2545   Following the bidirectionality of the system, we have that projections
2546   (the destructors of the record) infer the type, while the constructor
2547   gets checked:
2548   \begin{center}
2549     \mysmall
2550     \begin{tabular}{cc}
2551       \AxiomC{$\mychk{\mytmm}{\mytya}$}
2552       \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
2553       \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2554       \noLine
2555       \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
2556       \DisplayProof
2557       &
2558       \AxiomC{$\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2559       \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
2560       \noLine
2561       \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
2562       \DisplayProof
2563     \end{tabular}
2564   \end{center}
2565   What we have is equivalent to ITT's dependent products.
2566 \end{description}
2567
2568 \begin{figure}[p]
2569     \mydesc{syntax}{ }{
2570       \footnotesize
2571       $
2572       \begin{array}{l}
2573         \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2574       \end{array}
2575       $
2576     }
2577
2578     \mynegder
2579
2580   \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
2581     \footnotesize
2582       $
2583       \begin{array}{r@{\ }l}
2584          & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
2585         \myelabf &
2586         
2587         \begin{array}{r@{\ }c@{\ }l}
2588           \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
2589           \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
2590         \end{array}
2591       \end{array}
2592       $
2593   }
2594
2595     \mynegder
2596
2597   \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2598         \footnotesize
2599
2600       \AxiomC{$
2601         \begin{array}{c}
2602           \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2603           \mytyc{D} \not\in \myctx \\
2604           \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
2605           \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
2606         \end{array}
2607           $}
2608       \UnaryInfC{$
2609         \begin{array}{r@{\ }c@{\ }l}
2610           \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
2611           & & \vspace{-0.2cm} \\
2612           & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
2613           &          &
2614           \begin{array}{@{}r@{\ }l l}
2615             \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
2616             & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
2617             & \left.
2618               \begin{array}{@{}l}
2619                 \myind{3} \vdots \\
2620                 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
2621               \end{array} \right \}
2622             & \textbf{methods}  \\
2623             & \myapp{\myb{P}}{\myb{x}} &
2624           \end{array}
2625         \end{array}
2626         $}
2627       \DisplayProof \\ \vspace{0.2cm}\ \\
2628       $
2629         \begin{array}{@{}l l@{\ } l@{} r c l}
2630           \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2631           & \myhyps(\myb{P}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{\mytmt}}) \mycc \mytele &) & \mymetagoes & (\myb{r'} {:} \myapp{\myb{P}}{\myb{r}}) \mycc \myhyps(\myb{P}, \mytele) \\
2632           & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
2633         \end{array}
2634         $
2635
2636   }
2637
2638     \mynegder
2639
2640   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{  
2641         \footnotesize
2642         $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
2643       \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2644       \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
2645       \BinaryInfC{$
2646           \myctx \vdash \myapp{\myapp{\myapp{\mytyc{D}.\myfun{elim}}{(\myapp{\mytyc{D}.\mydc{c}_i}{\vec{\myse{t}}})}}{\myse{P}}}{\vec{\myse{m}}} \myred \myapp{\myapp{\myse{m}_i}{\vec{\mytmt}}}{\myrecs(\myse{P}, \vec{m}, \mytele_i)}
2647         $}
2648       \DisplayProof \\ \vspace{0.2cm}\ \\
2649       $
2650         \begin{array}{@{}l l@{\ } l@{} r c l}
2651           \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2652                          & \myrecs(\myse{P}, \vec{m}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{A}}); \mytele & ) & \mymetagoes &  (\mytyc{D}.\myfun{elim} \myappsp \myb{r} \myappsp \myse{P} \myappsp \vec{m}); \myrecs(\myse{P}, \vec{m}, \mytele) \\
2653                          & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
2654         \end{array}
2655         $
2656   }
2657
2658     \mynegder
2659
2660     \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
2661           \footnotesize
2662     $
2663     \begin{array}{r@{\ }c@{\ }l}
2664       \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2665              & \myelabf &
2666
2667              \begin{array}{r@{\ }c@{\ }l}
2668                \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \mytyc{D}.\mydc{constr} \myappsp \mytmsyn^{n} \mysynsep \cdots  \mysynsep \mytyc{D}.\myfun{f}_n \myappsp \mytmsyn \\
2669              \end{array}
2670     \end{array}
2671     $
2672 }
2673
2674     \mynegder
2675
2676 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2677       \footnotesize
2678     \AxiomC{$
2679       \begin{array}{c}
2680         \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2681         \mytyc{D} \not\in \myctx \\
2682         \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
2683       \end{array}
2684         $}
2685     \UnaryInfC{$
2686       \begin{array}{r@{\ }c@{\ }l}
2687         \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2688         & & \vspace{-0.2cm} \\
2689         & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\myfun{f}_n : \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \mysub{\myse{F}_n}{\myb{f}_i}{\myapp{\myfun{f}_i}{\myb{x}}}_{i = 1}^{n-1}; \\
2690         & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
2691       \end{array}
2692       $}
2693     \DisplayProof
2694 }
2695
2696     \mynegder
2697
2698   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2699         \footnotesize
2700           $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
2701           \AxiomC{$\mytyc{D} \in \myctx$}
2702           \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
2703           \DisplayProof
2704   }
2705
2706   \caption{Elaboration for data types and records.}
2707   \label{fig:elab}
2708 \end{figure}
2709
2710 Following the intuition given by the examples, the mechanised
2711 elaboration is presented in figure \ref{fig:elab}, which is essentially
2712 a modification of figure 9 of \citep{McBride2004}\footnote{However, our
2713   datatypes do not have indices, we do bidirectional typechecking by
2714   treating constructors/destructors as syntactic constructs, and we have
2715   records.}.
2716
2717 In data types declarations we allow recursive occurrences as long as
2718 they are \emph{strictly positive}, employing a syntactic check to make
2719 sure that this is the case.  See \cite{Dybjer1991} for a more formal
2720 treatment of inductive definitions in ITT.
2721
2722 For what concerns records, recursive occurrences are disallowed.  The
2723 reason for this choice is answered by the reason for the choice of
2724 having records at all: we need records to give the user types with
2725 $\eta$-laws for equality, as we saw in Section \ref{sec:eta-expand}
2726 and in the treatment of OTT in Section \ref{sec:ott}.  If we tried to
2727 $\eta$-expand recursive data types, we would expand forever.
2728
2729 To implement bidirectional type checking for constructors and
2730 destructors, we store their types in full in the context, and then
2731 instantiate when due:
2732
2733 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
2734     \AxiomC{$
2735       \begin{array}{c}
2736         \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
2737         \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
2738         \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
2739         \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
2740         \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \ 
2741           (1 \le i \le \mytele'')
2742       \end{array}
2743       $}
2744     \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
2745     \DisplayProof
2746
2747     \myderivspp
2748
2749     \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2750     \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
2751       \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
2752     \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
2753     \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
2754         \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
2755         \myse{F})(\vec{A};\mytmt)}$}
2756     \DisplayProof
2757   }
2758
2759 \subsubsection{Why user defined types?  Why eliminators?}
2760
2761 The `hardest' design choice when designing $\mykant$\ was to decide
2762 whether user defined types should be included.  In the end, we can
2763 devise
2764
2765 % TODO reference levitated theories, indexed containers
2766
2767 foobar
2768
2769 \subsection{Cumulative hierarchy and typical ambiguity}
2770 \label{sec:term-hierarchy}
2771
2772 Having a well founded type hierarchy is crucial if we want to retain
2773 consistency, otherwise we can break our type systems by proving bottom,
2774 as shown in Appendix \ref{sec:hurkens}.
2775
2776 However, hierarchy as presented in section \label{sec:itt} is a
2777 considerable burden on the user, on various levels.  Consider for
2778 example how we recovered disjunctions in Section \ref{sec:disju}: we
2779 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
2780 What if we wanted to form a disjunction containing something a
2781 $\mytyp_1$, or $\mytyp_{42}$?  Our definition would fail us, since
2782 $\mytyp_1 : \mytyp_2$.
2783
2784 \begin{figure}[b!]
2785
2786   % TODO finish
2787 \mydesc{cumulativity:}{\myctx \vdash \mytmsyn \mycumul \mytmsyn}{
2788   \begin{tabular}{ccc}
2789     \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
2790     \UnaryInfC{$\myctx \vdash \mytya \mycumul \mytyb$}
2791     \DisplayProof
2792     &
2793     \AxiomC{\phantom{$\myctx \vdash \mytya \mydefeq \mytyb$}}
2794     \UnaryInfC{$\myctx \vdash \mytyp_l \mycumul \mytyp_{l+1}$}
2795     \DisplayProof
2796     &
2797     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
2798     \AxiomC{$\myctx \vdash \mytyb \mycumul \myse{C}$}
2799     \BinaryInfC{$\myctx \vdash \mytya \mycumul \myse{C}$}
2800     \DisplayProof
2801   \end{tabular}
2802
2803   \myderivspp
2804
2805   \begin{tabular}{ccc}
2806     \AxiomC{$\myjud{\mytmt}{\mytya}$}
2807     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
2808     \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
2809     \DisplayProof
2810     &
2811     \AxiomC{$\myctx \vdash \mytya_1 \mydefeq \mytya_2$}
2812     \AxiomC{$\myctx; \myb{x} : \mytya_1 \vdash \mytyb_1 \mycumul \mytyb_2$}
2813     \BinaryInfC{$\myctx (\myb{x} {:} \mytya_1) \myarr \mytyb_1 \mycumul  (\myb{x} {:} \mytya_2) \myarr \mytyb_2$}
2814     \DisplayProof
2815   \end{tabular}
2816 }
2817 \caption{Cumulativity rules for base types in \mykant, plus a
2818   `conversion' rule for cumulative types.}
2819   \label{fig:cumulativity}
2820 \end{figure}
2821
2822 One way to solve this issue is a \emph{cumulative} hierarchy, where
2823 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$.  This way we retain
2824 consistency, while allowing for `large' definitions that work on small
2825 types too.  Figure \ref{fig:cumulativity} gives a formal definition of
2826 cumulativity for types, abstractions, and data constructors.
2827
2828 For example we might define our disjunction to be
2829 \[
2830   \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
2831 \]
2832 And hope that $\mytyp_{100}$ will be large enough to fit all the types
2833 that we want to use with our disjunction.  However, there are two
2834 problems with this.  First, there is the obvious clumsyness of having to
2835 manually specify the size of types.  More importantly, if we want to use
2836 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
2837 make sure that those allow for types at least as large as
2838 $\mytyp_{100}$.
2839
2840 A better option is to employ a mechanised version of what Russell called
2841 \emph{typical ambiguity}: we let the user live under the illusion that
2842 $\mytyp : \mytyp$, but check that the statements about types are
2843 consistent behind the hood.  $\mykant$\ implements this following the
2844 lines of \cite{Huet1988}.  See also \citep{Harper1991} for a published
2845 reference, although describing a more complex system allowing for both
2846 explicit and explicit hierarchy at the same time.
2847
2848 We define a partial ordering on the levels, with both weak ($\le$) and
2849 strong ($<$) constraints---the laws governing them being the same as the
2850 ones governing $<$ and $\le$ for the natural numbers.  Each occurrence
2851 of $\mytyp$ is decorated with a unique reference, and we keep a set of
2852 constraints and add new constraints as we type check, generating new
2853 references when needed.
2854
2855 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
2856 denotes the unique reference assigned to that term, we will generate a
2857 new fresh reference $\mytyp\, r_2$, and add the constraint $r_1 < r_2$
2858 to the set.  When type checking $\myctx \vdash
2859 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
2860 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
2861 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
2862 set.
2863
2864 If at any point the constraint set becomes inconsistent, type checking
2865 fails.  Moreover, when comparing two $\mytyp$ terms we equate their
2866 respective references with two $\le$ constraints---the details are
2867 explained in Section \ref{sec:hier-impl}.
2868
2869 Another more flexible but also more verbose alternative is the one
2870 chosen by Agda, where levels can be quantified so that the relationship
2871 between arguments and result in type formers can be explicitly
2872 expressed:
2873 \[
2874 \myarg\myfun{$\vee$}\myarg : (l_1\, l_2 : \mytyc{Level}) \myarr \mytyp_{l_1} \myarr \mytyp_{l_2} \myarr \mytyp_{l_1 \mylub l_2}
2875 \]
2876 Inference algorithms to automatically derive this kind of relationship
2877 are currently subject of research.  We chose less flexible but more
2878 concise way, since it is easier to implement and better understood.
2879
2880 % \begin{figure}[t]
2881 %   % TODO do this
2882 %   \caption{Constraints generated by the typical ambiguity engine.  We
2883 %     assume some global set of constraints with the ability of generating
2884 %     fresh references.}
2885 %   \label{fig:hierarchy}
2886 % \end{figure}
2887
2888 \subsection{Observational equality, \mykant\ style}
2889
2890 There are two correlated differences between $\mykant$\ and the theory
2891 used to present OTT.  The first is that in $\mykant$ we have a type
2892 hierarchy, which lets us, for example, abstract over types.  The second
2893 is that we let the user define inductive types.
2894
2895 Reconciling propositions for OTT and a hierarchy had already been
2896 investigated by Conor McBride,\footnote{See
2897   \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.} and we
2898 follow his broad design plan, although with some modifications.  Most of
2899 the work, as an extension of elaboration, is to handle reduction rules
2900 and coercions for data types---both type constructors and data
2901 constructors.
2902
2903 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
2904
2905 Before defining $\myprop$, we define some basic types inside $\mykant$,
2906 as the target for the $\myprop$ decoder:
2907 \[
2908 \begin{array}{l}
2909   \myadt{\mytyc{Empty}}{}{ }{ } \\
2910   \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
2911   \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
2912   \ \\
2913
2914   \myreco{\mytyc{Unit}}{}{}{ } \\ \ \\
2915
2916   \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
2917 \end{array}
2918 \]
2919 When using $\mytyc{Prod}$, we shall use $\myprod$ to define `nested'
2920 products, and $\myproj{n}$ to project elements from them, so that
2921 \[
2922 \begin{array}{@{}l}
2923 \mytya \myprod \mytyb = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myunit) \\
2924 \mytya \myprod \mytyb \myprod \myse{C} = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp (\mytyc{Prod} \myappsp \mytyc \myappsp \myunit)) \\
2925 \myind{2} \vdots \\
2926 \myproj{1} : \mytyc{Prod} \myappsp \mytya \myappsp \mytyb \myarr \mytya \\
2927 \myproj{2} : \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myse{C}) \myarr \mytyb \\
2928 \myind{2} \vdots
2929 \end{array}
2930 \]
2931 And so on, so that $\myproj{n}$ will work with all products with at
2932 least than $n$ elements.  Then we can define propositions, and decoding:
2933
2934 \mydesc{syntax}{ }{
2935   $
2936   \begin{array}{r@{\ }c@{\ }l}
2937     \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
2938     \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
2939   \end{array}
2940   $
2941 }
2942
2943 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2944   \begin{tabular}{cc}
2945     $
2946     \begin{array}{l@{\ }c@{\ }l}
2947       \myprdec{\mybot} & \myred & \myempty \\
2948       \myprdec{\mytop} & \myred & \myunit
2949     \end{array}
2950     $
2951     &
2952     $
2953     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2954       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2955       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2956       \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2957     \end{array}
2958     $
2959   \end{tabular}
2960 }
2961
2962 Adopting the same convention as with $\mytyp$-level products, we will
2963 nest $\myand$ in the same way.
2964
2965 \subsubsection{Some OTT examples}
2966
2967 Before presenting the direction that $\mykant$\ takes, let's consider
2968 some examples of use-defined data types, and the result we would expect,
2969 given what we already know about OTT, assuming the same propositional
2970 equalities.
2971
2972 \begin{description}
2973
2974 \item[Product types] Let's consider first the already mentioned
2975   dependent product, using the alternate name $\mysigma$\footnote{For
2976     extra confusion, `dependent products' are often called `dependent
2977     sums' in the literature, referring to the interpretation that
2978     identifies the first element as a `tag' deciding the type of the
2979     second element, which lets us recover sum types (disjuctions), as we
2980     saw in Section \ref{sec:user-type}.  Thus, $\mysigma$.} to
2981   avoid confusion with the $\mytyc{Prod}$ in the prelude:
2982   \[
2983   \begin{array}{@{}l}
2984     \myreco{\mysigma}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
2985   \end{array}
2986   \]
2987   Let's start with type-level equality.  The result we want is
2988   \[
2989     \begin{array}{@{}l}
2990       \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\
2991       \myind{2} \mytya_1 \myeq \mytya_2 \myand \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \myapp{\mytyb_1}{\myb{x_1}} \myeq \myapp{\mytyb_2}{\myb{x_2}}}
2992     \end{array}
2993   \]
2994   The difference here is that in the original presentation of OTT
2995   the type binders are explicit, while here $\mytyb_1$ and $\mytyb_2$
2996   functions returning types.  We can do this thanks to the type
2997   hierarchy, and this hints at the fact that heterogeneous equality will
2998   have to allow $\mytyp$ `to the right of the colon', and in fact this
2999   provides the solution to simplify the equality above.
3000
3001   If we take, just like we saw previously in OTT
3002   \[
3003     \begin{array}{@{}l}
3004       \myjm{\myse{f}_1}{\myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}}{\myse{f}_2}{\myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}} \myred \\
3005       \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3006            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3007            \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
3008          }}
3009     \end{array}
3010   \]
3011   Then we can simply take
3012   \[
3013     \begin{array}{@{}l}
3014       \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\ \myind{2} \mytya_1 \myeq \mytya_2 \myand \myjm{\mytyb_1}{\mytya_1 \myarr \mytyp}{\mytyb_2}{\mytya_2 \myarr \mytyp}
3015     \end{array}
3016   \]
3017   Which will reduce to precisely what we desire.  For what
3018   concerns coercions and quotation, things stay the same (apart from the
3019   fact that we apply to the second argument instead of substituting).
3020   We can recognise records such as $\mysigma$ as such and employ
3021   projections in value equality and coercions; as to not
3022   impede progress if not necessary.
3023
3024 \item[Lists] Now for finite lists, which will give us a taste for data
3025   constructors:
3026   \[
3027   \begin{array}{@{}l}
3028     \myadt{\mylist}{\myappsp (\myb{A} {:} \mytyp)}{ }{\mydc{nil} \mydcsep \mydc{cons} \myappsp \myb{A} \myappsp (\myapp{\mylist}{\myb{A}})}
3029   \end{array}
3030   \]
3031   Type equality is simple---we only need to compare the parameter:
3032   \[
3033     \mylist \myappsp \mytya_1 \myeq \mylist \myappsp \mytya_2 \myred \mytya_1 \myeq \mytya_2
3034     \]
3035     For coercions, we transport based on the constructor, recycling the
3036     proof for the inductive occurrence:
3037   \[
3038     \begin{array}{@{}l@{\ }c@{\ }l}
3039       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mydc{nil} & \myred & \mydc{nil} \\
3040       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp (\mydc{cons} \myappsp \mytmm \myappsp \mytmn) & \myred & \\
3041       \multicolumn{3}{l}{\myind{2} \mydc{cons} \myappsp (\mycoe \myappsp \mytya_1 \myappsp \mytya_2 \myappsp \myse{Q} \myappsp \mytmm) \myappsp (\mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mytmn)}
3042     \end{array}
3043   \]
3044   Value equality is unsurprising---we match the constructors, and
3045   return bottom for mismatches.  However, we also need to equate the
3046   parameter in $\mydc{nil}$:
3047   \[
3048     \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
3049       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mytya_1 \myeq \mytya_2 \\
3050       (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \\
3051       & \multicolumn{11}{@{}l}{ \myind{2}
3052         \myjm{\mytmm_1}{\mytya_1}{\mytmm_2}{\mytya_2} \myand \myjm{\mytmn_1}{\myapp{\mylist}{\mytya_1}}{\mytmn_2}{\myapp{\mylist}{\mytya_2}}
3053         } \\
3054       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot \\
3055       (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot
3056     \end{array}
3057   \]
3058   % TODO quotation
3059
3060 \item[Evil type]
3061   Now for something useless but complicated.
3062
3063 \end{description}
3064
3065 \subsubsection{Only one equality}
3066
3067 Given the examples above, a more `flexible' heterogeneous emerged, since
3068 of the fact that in $\mykant$ we re-gain the possibility of abstracting
3069 and in general handling sets in a way that was not possible in the
3070 original OTT presentation.  Moreover, we found that the rules for value
3071 equality work very well if used with user defined type
3072 abstractions---for example in the case of dependent products we recover
3073 the original definition with explicit binders, in a very simple manner.
3074
3075 In fact, we can drop a separate notion of type-equality, which will
3076 simply be served by $\myjm{\mytya}{\mytyp}{\mytyb}{\mytyp}$, from now on
3077 abbreviated as $\mytya \myeq \mytyb$.  We shall still distinguish
3078 equalities relating types for hierarchical purposes.  The full rules for
3079 equality reductions, along with the syntax for propositions, are given
3080 in figure \ref{fig:kant-eq-red}.  We exploit record to perform
3081 $\eta$-expansion.  Moreover, given the nested $\myand$s, values of data
3082 types with zero constructors (such as $\myempty$) and records with zero
3083 destructors (such as $\myunit$) will be automatically always identified
3084 as equal.
3085
3086 \begin{figure}[p]
3087 \mydesc{syntax}{ }{
3088   \small
3089   $
3090   \begin{array}{r@{\ }c@{\ }l}
3091     \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3092   \end{array}
3093   $
3094 }
3095
3096     % \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
3097     %                  \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3098     % \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3099
3100 % \mynegder
3101
3102 % \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
3103 %   \small
3104 %   \begin{tabular}{cc}
3105 %     \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3106 %     \AxiomC{$\myjud{\mytmt}{\mytya}$}
3107 %     \BinaryInfC{$\myinf{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
3108 %     \DisplayProof
3109 %     &
3110 %     \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3111 %     \AxiomC{$\myjud{\mytmt}{\mytya}$}
3112 %     \BinaryInfC{$\myinf{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
3113 %     \DisplayProof
3114 %   \end{tabular}
3115 % }
3116
3117 \mynegder
3118
3119 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
3120   \small
3121     \begin{tabular}{cc}
3122       \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
3123       \UnaryInfC{$\myjud{\mytop}{\myprop}$}
3124       \noLine
3125       \UnaryInfC{$\myjud{\mybot}{\myprop}$}
3126       \DisplayProof
3127       &
3128       \AxiomC{$\myjud{\myse{P}}{\myprop}$}
3129       \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
3130       \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
3131       \noLine
3132       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
3133       \DisplayProof
3134     \end{tabular}
3135
3136     \myderivspp
3137
3138     \begin{tabular}{cc}
3139       \AxiomC{$
3140         \begin{array}{@{}c}
3141           \phantom{\myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}}} \\
3142           \myjud{\myse{A}}{\mytyp}\hspace{0.8cm}
3143           \myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}
3144         \end{array}
3145         $}
3146       \UnaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
3147       \DisplayProof
3148       &
3149       \AxiomC{$
3150         \begin{array}{c}
3151           \myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}} \\
3152           \myjud{\myse{B}}{\mytyp} \hspace{0.8cm} \myjud{\mytmn}{\myse{B}}
3153         \end{array}
3154         $}
3155       \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
3156       \DisplayProof
3157     \end{tabular}
3158 }
3159
3160 \mynegder
3161   % TODO equality for decodings
3162 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
3163   \small
3164     \begin{tabular}{cc}
3165     \AxiomC{}
3166     \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
3167     \DisplayProof
3168     &
3169     \AxiomC{}
3170     \UnaryInfC{$\myctx \vdash \myjm{\myprdec{\myse{P}}}{\mytyp}{\myprdec{\myse{Q}}}{\mytyp} \myred \mytop$}
3171     \DisplayProof
3172     \end{tabular}
3173
3174   \myderivspp
3175
3176   \AxiomC{}
3177   \UnaryInfC{$
3178     \begin{array}{@{}r@{\ }l}
3179     \myctx \vdash &
3180     \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp}  \myred \\
3181     & \myind{2} \mytya_2 \myeq \mytya_1 \myand \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
3182         \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]
3183       }}
3184     \end{array}
3185     $}
3186   \DisplayProof
3187
3188   \myderivspp
3189
3190   \AxiomC{}
3191   \UnaryInfC{$
3192     \begin{array}{@{}r@{\ }l}
3193       \myctx \vdash &
3194       \myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}  \myred \\
3195       & \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3196           \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3197           \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
3198         }}
3199     \end{array}
3200     $}
3201   \DisplayProof
3202   
3203
3204   \myderivspp
3205
3206   \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3207   \UnaryInfC{$
3208     \begin{array}{r@{\ }l}
3209       \myctx \vdash &
3210       \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp}  \myred \\
3211       & \myind{2} \mybigand_{i = 1}^n (\myjm{\mytya_n}{\myhead(\mytele(A_1 \cdots A_{i-1}))}{\mytyb_i}{\myhead(\mytele(B_1 \cdots B_{i-1}))})
3212     \end{array}
3213     $}
3214   \DisplayProof
3215
3216   \myderivspp
3217
3218   \AxiomC{$
3219     \begin{array}{@{}c}
3220       \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
3221       \mytyc{D}.\mydc{c} : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \hspace{0.8cm}
3222       \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
3223       \mytele_B = (\mytele;\mytele')\vec{B}
3224     \end{array}
3225     $}
3226   \UnaryInfC{$
3227     \begin{array}{@{}l@{\ }l}
3228       \myctx \vdash & \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \\
3229       & \myind{2} \mybigand_{i=1}^n(\myjm{\mytmm_i}{\myhead(\mytele_A (\mytya_i \cdots \mytya_{i-1}))}{\mytmn_i}{\myhead(\mytele_B (\mytyb_i \cdots \mytyb_{i-1}))})
3230     \end{array}
3231     $}
3232   \DisplayProof
3233
3234   \myderivspp
3235
3236   \AxiomC{$\mydataty(\mytyc{D}, \myctx)$}
3237   \UnaryInfC{$
3238       \myctx \vdash \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c'} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \mybot
3239     $}
3240   \DisplayProof
3241
3242   \myderivspp
3243
3244   \AxiomC{$
3245     \begin{array}{@{}c}
3246       \myisreco(\mytyc{D}, \myctx)\hspace{0.8cm}
3247       \mytyc{D}.\myfun{f}_i : \mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i  \in \myctx\\
3248     \end{array}
3249     $}
3250   \UnaryInfC{$
3251     \begin{array}{@{}l@{\ }l}
3252       \myctx \vdash & \myjm{\myse{l}}{\mytyc{D} \myappsp \vec{A}}{\myse{r}}{\mytyc{D} \myappsp \vec{B}} \myred \\ & \myind{2} \mybigand_{i=1}^n(\myjm{\mytyc{D}.\myfun{f}_1 \myappsp \myse{l}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytya};\myse{l})}{\mytyc{D}.\myfun{f}_i \myappsp \myse{r}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytyb};\myse{r})})
3253     \end{array}
3254     $}
3255   \DisplayProof
3256   
3257   \myderivspp
3258   \AxiomC{}
3259   \UnaryInfC{$\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb}  \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
3260   \DisplayProof
3261 }
3262 \caption{Propositions and equality reduction in $\mykant$.  We assume
3263   the presence of $\mydataty$ and $\myisreco$ as operations on the
3264   context to recognise whether a user defined type is a data type or a
3265   record.}
3266   \label{fig:kant-eq-red}
3267 \end{figure}
3268
3269 \subsubsection{Coercions}
3270
3271 % \begin{figure}[t]
3272 %   \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
3273
3274 %   }
3275 %   \caption{Coercions in \mykant.}
3276 %   \label{fig:kant-coe}
3277 % \end{figure}
3278
3279 % TODO finish
3280
3281 \subsubsection{$\myprop$ and the hierarchy}
3282
3283 Where is $\myprop$ placed in the type hierarchy?  The main indicator
3284 is the decoding operator, since it converts into things that already
3285 live in the hierarchy.  For example, if we
3286 have
3287 \[
3288   \myprdec{\mynat \myarr \mybool \myeq \mynat \myarr \mybool} \myred
3289   \mytop \myand ((\myb{x}\, \myb{y} : \mynat) \myarr \mytop \myarr \mytop)
3290 \]
3291 we will better make sure that the `to be decoded' is at the same
3292 level as its reduction as to preserve subject reduction.  In the example
3293 above, we'll have that proposition to be at least as large as the type
3294 of $\mynat$, since the reduced proof will abstract over it.  Pretending
3295 that we had explicit, non cumulative levels, it would be tempting to have
3296 \begin{center}
3297 \begin{tabular}{cc}
3298   \AxiomC{$\myjud{\myse{Q}}{\myprop_l}$}
3299   \UnaryInfC{$\myjud{\myprdec{\myse{Q}}}{\mytyp_l}$}
3300   \DisplayProof
3301 &
3302   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3303   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3304   \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3305   \DisplayProof
3306 \end{tabular}
3307 \end{center}
3308 $\mybot$ and $\mytop$ living at any level, $\myand$ and $\forall$
3309 following rules similar to the ones for $\myprod$ and $\myarr$ in
3310 Section \ref{sec:itt}. However, we need to be careful with value
3311 equality since for example we have that
3312 \[
3313   \myprdec{\myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}}
3314   \myred
3315   \myfora{\myb{x_1}}{\mytya_1}{\myfora{\myb{x_2}}{\mytya_2}{\cdots}}
3316 \]
3317 where the proposition decodes into something of type $\mytyp_l$, where
3318 $\mytya : \mytyp_l$ and $\mytyb : \mytyp_l$.  We can resolve this
3319 tension by making all equalities larger:
3320 \begin{prooftree}
3321   \AxiomC{$\myjud{\mytmm}{\mytya}$}
3322   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3323   \AxiomC{$\myjud{\mytmn}{\mytyb}$}
3324   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3325   \QuaternaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
3326 \end{prooftree}
3327 This is disappointing, since type equalities will be needlessly large:
3328 $\myprdec{\myjm{\mytya}{\mytyp_l}{\mytyb}{\mytyp_l}} : \mytyp_{l + 1}$.
3329
3330 However, considering that our theory is cumulative, we can do better.
3331 Assuming rules for $\myprop$ cumulativity similar to the ones for
3332 $\mytyp$, we will have (with the conversion rule reproduced as a
3333 reminder):
3334 \begin{center}
3335   \begin{tabular}{cc}
3336     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3337     \AxiomC{$\myjud{\mytmt}{\mytya}$}
3338     \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
3339     \DisplayProof
3340     &
3341     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3342     \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3343     \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3344     \DisplayProof
3345   \end{tabular}
3346
3347   \myderivspp
3348
3349   \AxiomC{$\myjud{\mytmm}{\mytya}$}
3350   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3351   \AxiomC{$\myjud{\mytmn}{\mytyb}$}
3352   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3353   \AxiomC{$\mytya$ and $\mytyb$ are not $\mytyp_{l'}$}
3354   \QuinaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
3355   \DisplayProof
3356 \end{center}
3357
3358 That is, we are small when we can (type equalities) and large otherwise.
3359 This would not work in a non-cumulative theory because subject reduction
3360 would not hold.  Consider for instance
3361 \[
3362   \myjm{\mynat}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}{\mybool}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}
3363   : \myprop_1
3364 \]
3365 which reduces to
3366 \[\myjm{\mynat}{\mytyp_0}{\mybool}{\mytyp_0} : \myprop_0 \]
3367 We need $\myprop_0$ to be $\myprop_1$ too, which will be the case with
3368 cumulativity.  This is not the most elegant of systems, but it buys us a
3369 cheap type level equality without having to replicate functionality with
3370 a dedicated construct.
3371
3372 \subsubsection{Quotation and definitional equality}
3373 \label{sec:kant-irr}
3374
3375 Now we can give an account of definitional equality, by explaining how
3376 to perform quotation (as defined in Section \ref{sec:eta-expand})
3377 towards the goal described in Section \ref{sec:ott-quot}.
3378
3379 We want to:
3380 \begin{itemize}
3381 \item Perform $\eta$-expansion on functions and records.
3382
3383 \item As a consequence of the previous point, identify all records with
3384 no projections as equal, since they will have only one element.
3385
3386 \item Identify all members of types with no elements as equal.
3387
3388 \item Identify all equivalent proofs as equal---with `equivalent proof'
3389 we mean those proving the same propositions.
3390
3391 \item Advance coercions working across definitionally equal types.
3392 \end{itemize}
3393 Towards these goals and following the intuition between bidirectional
3394 type checking we define two mutually recursive functions, one quoting
3395 canonical terms against their types (since we need the type to typecheck
3396 canonical terms), one quoting neutral terms while recovering their
3397 types.
3398
3399 Our quotation will work on normalised terms, so that all defined values
3400 will have been replaced.  Moreover, we match on datatype eliminators and
3401 all their arguments, so that $\mynat.\myfun{elim} \myappsp \vec{\mytmt}$
3402 will stand for $\mynat.\myfun{elim}$ applied to the scrutinised
3403 $\mynat$, plus its three arguments.  This measure can be easily
3404 implemented by checking the head of applications and `consuming' the
3405 needed terms.
3406
3407 % TODO finish this
3408 \begin{figure}[t]
3409   \mydesc{canonical quotation}{\myctx \vdash \mytmsyn \myquot \mytmsyn \mapsto \mytmsyn}{
3410                           
3411   }
3412
3413   \mynegder
3414
3415   \mydesc{neutral quotation}{\myctx \vdash \mynquot \mytmsyn  \mapsto \mytmsyn : \mytmsyn}{
3416                     
3417   }
3418   \caption{Quotation in \mykant.}
3419   \label{fig:kant-quot}
3420 \end{figure}
3421
3422 % TODO finish
3423
3424 \subsubsection{Why $\myprop$?}
3425
3426 It is worth to ask if $\myprop$ is needed at all.  It is perfectly
3427 possible to have the type checker identify propositional types
3428 automatically, and in fact in some sense we already do during equality
3429 reduction and quotation.  However, this has the considerable
3430 disadvantage that we can never identify abstracted
3431 variables\footnote{And in general neutral terms, although we currently
3432   don't have neutral propositions.} of type $\mytyp$ as $\myprop$, thus
3433 forbidding the user to talk about $\myprop$ explicitly.
3434
3435 This is a considerable impediment, for example when implementing
3436 \emph{quotient types}.  With quotients, we let the user specify an
3437 equivalence class over a certain type, and then exploit this in various
3438 way---crucially, we need to be sure that the equivalence given is
3439 propositional, a fact which prevented the use of quotients in dependent
3440 type theories \citep{Jacobs1994}.
3441
3442 % TODO finish
3443
3444 \section{\mykant : The practice}
3445 \label{sec:kant-practice}
3446
3447 The codebase consists of around 2500 lines of Haskell, as reported by
3448 the \texttt{cloc} utility.  The high level design is inspired by the
3449 work on various incarnations of Epigram, and specifically by the first
3450 version as described \citep{McBride2004} and the codebase for the new
3451 version.\footnote{Available intermittently as a \texttt{darcs}
3452 repository at \url{http://sneezy.cs.nott.ac.uk/darcs/Pig09}.}  In many
3453 ways \mykant\ is something in between the first and second version of
3454 Epigram.
3455
3456 The author learnt the hard way the implementations challenges for such a
3457 project, and while there is a solid and working base to work on, the
3458 implementation of observational equality is not currently complete.
3459 However, given the detailed plan in the previous section, doing so
3460 should not prove to be too much work.
3461
3462 The interaction happens in a read-eval-print loop (REPL).  The REPL is a
3463 available both as a commandline application and in a web interface,
3464 which is available at \url{kant.mazzo.li} and presents itself as in
3465 figure \ref{fig:kant-web}.
3466
3467 \begin{figure}[t]
3468 {\small\begin{verbatim}B E R T U S
3469 Version 0.0, made in London, year 2013.
3470 >>> :h
3471 <decl>     Declare value/data type/record
3472 :t <term>  Typecheck
3473 :e <term>  Normalise
3474 :p <term>  Pretty print
3475 :l <file>  Load file
3476 :r <file>  Reload file (erases previous environment)
3477 :i <name>  Info about an identifier
3478 :q         Quit
3479 >>> :l data/samples/good/common.ka 
3480 OK
3481 >>> :e plus three two
3482 suc (suc (suc (suc (suc zero))))
3483 >>> :t plus three two
3484 Type: Nat\end{verbatim}
3485 }
3486
3487   \caption{A sample run of the \mykant\ prompt.}
3488   \label{fig:kant-web}
3489 \end{figure}
3490
3491 The interaction with the user takes place in a loop living in and updating a
3492 context \mykant\ declarations.  The user inputs a new declaration that goes
3493 through various stages starts with the user inputing a \mykant\ declaration or
3494 another REPL command, which then goes through various stages that can end up
3495 in a context update, or in failures of various kind.  The process is described
3496 diagrammatically in figure \ref{fig:kant-process}:
3497
3498 \begin{description}
3499 \item[Parse] In this phase the text input gets converted to a sugared
3500   version of the core language.
3501
3502 \item[Desugar] The sugared declaration is converted to a core term.
3503
3504 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
3505   which is necessary to implement the type hierarchy check.
3506
3507 \item[Elaborate] Converts the declaration to some context items, which
3508   might be a value declaration (type and body) or a data type
3509   declaration (constructors and destructors).  This phase works in
3510   tandem with \textbf{Type checking}, which in turns needs to
3511   \textbf{Evaluate} terms.
3512
3513 \item[Distill] and report the result.  `Distilling' refers to the process of
3514   converting a core term back to a sugared version that the user can
3515   visualise.  This can be necessary both to display errors including terms or
3516   to display result of evaluations or type checking that the user has
3517   requested.
3518
3519 \item[Pretty print] Format the terms in a nice way, and display the result to
3520   the user.
3521
3522 \end{description}
3523
3524 \begin{figure}
3525   \centering{\mysmall
3526     \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
3527     corners, minimum height=2.5em, node distance=0.7cm]
3528       
3529       \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
3530       centered, inner sep=0pt, node distance=0.7cm]
3531       
3532       \tikzstyle{line} = [draw, -latex']
3533       
3534       \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
3535       centered, node distance=1.5cm]
3536       
3537       
3538       \begin{tikzpicture}[auto]
3539         \node [cloud] (user) {User};
3540         \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
3541         \node [block, below=of parse] (desugar) {Desugar};
3542         \node [block, below=of desugar] (reference) {Reference};
3543         \node [block, below=of reference] (elaborate) {Elaborate};
3544         \node [block, left=of elaborate] (tycheck) {Typecheck};
3545         \node [block, left=of tycheck] (evaluate) {Evaluate};
3546         \node [decision, right=of elaborate] (error) {Error?};
3547         \node [block, right=of parse] (distill) {Distill};
3548         \node [block, right=of desugar] (update) {Update context};
3549         
3550         \path [line] (user) -- (parse);
3551         \path [line] (parse) -- (desugar);
3552         \path [line] (desugar) -- (reference);
3553         \path [line] (reference) -- (elaborate);
3554         \path [line] (elaborate) edge[bend right] (tycheck);
3555         \path [line] (tycheck) edge[bend right] (elaborate);
3556         \path [line] (elaborate) -- (error);
3557         \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
3558         \path [line] (error) -- node [near start] {no} (update);
3559         \path [line] (update) -- (distill);
3560         \path [line] (distill) -- (user);
3561         \path [line] (tycheck) edge[bend right] (evaluate);
3562         \path [line] (evaluate) edge[bend right] (tycheck);
3563       \end{tikzpicture}
3564   }
3565   \caption{High level overview of the life of a \mykant\ prompt cycle.}
3566   \label{fig:kant-process}
3567 \end{figure}
3568
3569 Here we will review only a sampling of the more interesting
3570 implementation challenges present when implementing an interactive
3571 theorem prover.
3572
3573 \subsection{Term and context representation}
3574 \label{sec:term-repr}
3575
3576 \subsubsection{Naming and substituting}
3577
3578 Perhaps surprisingly, one of the most fundamental challenges in
3579 implementing a theory of the kind presented is choosing a good data type
3580 for terms, and specifically handling substitutions in a sane way.
3581
3582 There are two broad schools of thought when it comes to naming
3583 variables, and thus substituting:
3584 \begin{description}
3585 \item[Nameful] Bound variables are represented by some enumerable data
3586   type, just as we have described up to now, starting from Section
3587   \ref{sec:untyped}.  The problem is that avoiding name capturing is a
3588   nightmare, both in the sense that it is not performant---given that we
3589   need to rename rename substitute each time we `enter' a binder---but
3590   most importantly given the fact that in even more slightly complicated
3591   systems it is very hard to get right, even for experts.
3592
3593   One of the sore spots of explicit names is comparing terms up
3594   $\alpha$-renaming, which again generates a huge amounts of
3595   substitutions and requires special care.  We can represent the
3596   relationship between variables and their binders, by...
3597
3598 \item[Nameless] ...getting rid of names altogether, and representing
3599   bound variables with an index referring to the `binding' structure, a
3600   notion introduced by \cite{de1972lambda}.  Classically $0$ represents
3601   the variable bound by the innermost binding structure, $1$ the
3602   second-innermost, and so on.  For instance with simple abstractions we
3603   might have
3604   \[\mymacol{red}{\lambda}\, (\mymacol{blue}{\lambda}\, \mymacol{blue}{0}\, (\mymacol{AgdaInductiveConstructor}{\lambda\, 0}))\, (\mymacol{AgdaFunction}{\lambda}\, \mymacol{red}{1}\, \mymacol{AgdaFunction}{0}) : ((\mytya \myarr \mytya) \myarr \mytyb) \myarr \mytyb \]
3605
3606   While this technique is obviously terrible in terms of human
3607   usability,\footnote{With some people going as far as defining it akin
3608   to an inverse Turing test.} it is much more convenient as an
3609   internal representation to deal with terms mechanically---at least in
3610   simple cases.  Moreover, $\alpha$ renaming ceases to be an issue, and
3611   term comparison is purely syntactical.
3612
3613   Nonetheless, more complex, more complex constructs such as pattern
3614   matching put some strain on the indices and many systems end up using
3615   explicit names anyway (Agda, Coq, \dots).
3616
3617 \end{description}
3618
3619 In the past decade or so advancements in the Haskell's type system and
3620 in general the spread new programming practices have enabled to make the
3621 second option much more amenable.  \mykant\ thus takes the second path
3622 through the use of Edward Kmett's excellent \texttt{bound}
3623 library.\footnote{Available at
3624 \url{http://hackage.haskell.org/package/bound}.}  We decribe its
3625 advantages but also pitfalls in the previously relatively unknown
3626 territory of dependent types---\texttt{bound} being created mostly to
3627 handle more simply typed systems.
3628
3629 \texttt{bound} builds on two core ideas.  The first is the suggestion by
3630 \cite{Bird1999} consists of parametrising the term type over the type of
3631 the variables, and `nest' the type each time we enter a scope.  If we
3632 wanted to define a term for the untyped $\lambda$-calculus, we might
3633 have
3634 \begin{Verbatim}
3635 data Void
3636
3637 data Var v = Bound | Free v
3638
3639 data Tm v
3640     = V v               -- Bound variable
3641     | App (Tm v) (Tm v) -- Term application
3642     | Lam (Tm (Var v))  -- Abstraction
3643 \end{Verbatim}
3644 Closed terms would be of type \texttt{Tm Void}, so that there would be
3645 no occurrences of \texttt{V}.  However, inside an abstraction, we can
3646 have \texttt{V Bound}, representing the bound variable, and inside a
3647 second abstraction we can have \texttt{V Bound} or \texttt{V (Free
3648 Bound)}.  Thus the term $\myabs{\myb{x}}{\myabs{\myb{y}}{\myb{x}}}$ could be represented as
3649 \begin{Verbatim}
3650 Lam (Lam (Free Bound))
3651 \end{Verbatim}
3652 This allows us to reflect at the type level the `nestedness' of a type,
3653 and since we usually work with functions polymorphic on the parameter
3654 \texttt{v} it's very hard to make mistakes by putting terms of the wrong
3655 nestedness where they don't belong.
3656
3657 Even more interestingly, the substitution operation is perfectly
3658 captured by the \verb|>>=| (bind) operator of the \texttt{Monad}
3659 typeclass:
3660 \begin{Verbatim}
3661 class Monad m where
3662   return :: m a
3663   (>>=)  :: m a -> (a -> m b) -> m b
3664
3665 instance Monad Tm where
3666   -- `return'ing turns a variable into a `Tm'
3667   return = V
3668
3669   -- `t >>= f' takes a term `t' and a mapping from variables to terms
3670   -- `f' and applies `f' to all the variables in `t', replacing them
3671   -- with the mapped terms.
3672   V v     >>= f = f v
3673   App m n >>= f = App (m >>= f) (n >>= f)
3674
3675   -- `Lam' is the tricky case: we modify the function to work with bound
3676   -- variables, so that if it encounters `Bound' it leaves it untouched
3677   -- (since the mapping referred to the outer scope); if it encounters a
3678   -- free variable it asks `f' for the term and then updates all the
3679   -- variables to make them refer to the outer scope they were meant to
3680   -- be in.
3681   Lam s   >>= f = Lam (s >>= bump)
3682     where bump Bound    = return Bound
3683           bump (Free v) = f v >>= V . Free
3684 \end{Verbatim}
3685 With this in mind, we can define functions which will not only work on
3686 \verb|Tm|, but on any \verb|Monad|!
3687 \begin{Verbatim}
3688 -- Replaces free variable `v' with `m' in `n'.
3689 subst :: (Eq v, Monad m) => v -> m v -> m v -> m v
3690 subst v m n = n >>= \v' -> if v == v' then m else return v'
3691
3692 -- Replace the variable bound by `s' with term `t'.
3693 inst :: Monad m => m v -> m (Var v) -> m v
3694 inst t s = do v <- s
3695               case v of
3696                 Bound   -> t
3697                 Free v' -> return v'
3698 \end{Verbatim}
3699 The beauty of this technique is that in a few simple function we have
3700 defined all the core operations in a general and `obviously correct'
3701 way, with the extra confidence of having the type checker looking our
3702 back.
3703
3704 Moreover, if we take the top level term type to be \verb|Tm String|, we
3705 get for free a representation of terms with top-level, definitions;
3706 where closed terms contain only \verb|String| references to said
3707 definitions---see also \cite{McBride2004b}.
3708
3709 What are then the pitfalls of this seemingly invincible technique?  The
3710 most obvious impediment is the need for polymorphic recursion.
3711 Functions traversing terms parametrised by the variable type will have
3712 types such as
3713 \begin{Verbatim}
3714 -- Infer the type of a term, or return an error.
3715 infer :: Tm v -> Either Error (Tm v)
3716 \end{Verbatim}
3717 When traversing under a \verb|Scope| the parameter changes from \verb|v|
3718 to \verb|Var v|, and thus if we do not specify the type for our function explicitly
3719 inference will fail---type inference for polymorphic recursion being
3720 undecidable \citep{henglein1993type}.  This causes some annoyance,
3721 especially in the presence of many local definitions that we would like
3722 to leave untyped.
3723
3724 But the real issue is the fact that giving a type parametrised over a
3725 variable---say \verb|m v|---a \verb|Monad| instance means being able to
3726 only substitute variables for values of type \verb|m v|.  This is a
3727 considerable inconvenience.  Consider for instance the case of
3728 telescopes, which are a central tool to deal with contexts and other
3729 constructs:
3730 \begin{Verbatim}
3731 data Tele m v = End (m v) | Bind (m v) (Tele (Var v))
3732 type TeleTm = Tele Tm
3733 \end{Verbatim}
3734 The problem here is that what we want to substitute for variables in
3735 \verb|Tele m v| is \verb|m v| (probably \verb|Tm v|), not \verb|Tele m v| itself!  What we need is
3736 \begin{Verbatim}
3737 bindTele  :: Monad m => Tele m a -> (a -> m b) -> Tele m b
3738 substTele :: (Eq v, Monad m) => v -> m v -> Tele m v -> Tele m v
3739 instTele  :: Monad m => m v -> Tele m (Var v) -> Tele m v
3740 \end{Verbatim}
3741 Not what \verb|Monad| gives us.  Solving this issue in an elegant way
3742 has been a major sink of time and source of headaches for the author,
3743 who analysed some of the alternatives---most notably the work by
3744 \cite{weirich2011binders}---but found it impossible to give up the
3745 simplicity of the model above.
3746
3747 That said, our term type is still reasonably brief, as shown in full in
3748 Figure \ref{fig:term}.  The fact that propositions cannot be factored
3749 out in another data type is a consequence of the problems described
3750 above.  However the real pain is during elaboration, where we are forced
3751 to treat everything as a type while we would much rather have
3752 telescopes.  Future work would include writing a library that marries a
3753 nice interface similar to the one of \verb|bound| with a more flexible
3754 interface.
3755
3756 \begin{figure}[t]
3757 {\small\begin{verbatim}-- A top-level name.
3758 type Id    = String
3759 -- A data/type constructor name.
3760 type ConId = String
3761
3762 -- A term, parametrised over the variable (`v') and over the reference
3763 -- type used in the type hierarchy (`r').
3764 data Tm r v
3765     = V v                        -- Variable.
3766     | Ty r                       -- Type, with a hierarchy reference.
3767     | Lam (TmScope r v)          -- Abstraction.
3768     | Arr (Tm r v) (TmScope r v) -- Dependent function.
3769     | App (Tm r v) (Tm r v)      -- Application.
3770     | Ann (Tm r v) (Tm r v)      -- Annotated term.
3771       -- Data constructor, the first ConId is the type constructor and
3772       -- the second is the data constructor.
3773     | Con ADTRec ConId ConId [Tm r v]
3774       -- Data destrutor, again first ConId being the type constructor
3775       -- and the second the name of the eliminator.
3776     | Destr ADTRec ConId Id (Tm r v)
3777       -- A type hole.
3778     | Hole HoleId [Tm r v]
3779       -- Decoding of propositions.
3780     | Dec (Tm r v)
3781
3782       -- Propositions.
3783     | Prop r -- The type of proofs, with hierarchy reference.
3784     | Top
3785     | Bot
3786     | And (Tm r v) (Tm r v)
3787     | Forall (Tm r v) (TmScope r v)
3788       -- Heterogeneous equality.
3789     | Eq (Tm r v) (Tm r v) (Tm r v) (Tm r v)
3790
3791 -- Either a data type, or a record.
3792 data ADTRec = ADT | Rec
3793
3794 -- Either a coercion, or coherence.
3795 data Coeh = Coe | Coh\end{verbatim}
3796 }
3797   \caption{Data type for terms in \mykant.}
3798   \label{fig:term}
3799 \end{figure}
3800
3801 We also make use of a `forgetful' data type (as provided by
3802 \verb|bound|) to store user-provided variables names along with the
3803 `nameless' representation, so that the names will not be considered when
3804 compared terms, but will be available when distilling so that we can
3805 recover variable names that are as close as possible to what the user
3806 originally used.
3807
3808 \subsubsection{Context}
3809
3810 % TODO finish
3811
3812 \subsection{Type hierarchy}
3813 \label{sec:hier-impl}
3814
3815 As a breath of air amongst the type gas, we will explain how to
3816 implement the typical ambiguity we have spoken about in
3817 \ref{sec:term-hierarchy}.  As mentioned, we have to verify a the
3818 consistency of a set of constraints each time we add a new one.  The
3819 constraints range over some set of variables whose members we will
3820 denote with $x, y, z, \dots$.  and are of two kinds:
3821 \begin{center}
3822   \begin{tabular}{cc}
3823      $x \le y$ & $x < y$
3824   \end{tabular}
3825 \end{center}
3826
3827 Predictably, $\le$ expresses a reflexive order, and $<$ expresses an
3828 irreflexive order, both working with the same notion of equality, where
3829 $x < y$ implies $x \le y$---they behave like $\le$ and $<$ do on natural
3830 numbers (or in our case, levels in a type hierarchy).  We also need an
3831 equality constraint ($x = y$), which can be reduced to two constraints
3832 $x \le y$ and $y \le x$.
3833
3834 Given this specification, we have implemented a standalone Haskell
3835 module---that we plan to release as a library---to efficiently store and
3836 check the consistency of constraints.  Its interface is shown in Figure
3837 \ref{fig:constraint}.  The problem unpredictably reduces to a graph
3838 algorithm.  If we 
3839
3840 \begin{figure}[t]
3841 {\small\begin{verbatim}module Data.Constraint where
3842
3843 -- | Data type holding the set of constraints, parametrised over the
3844 --   type of the variables.
3845 data Constrs a
3846
3847 -- | A representation of the constraints that we can add.
3848 data Constr a = a :<=: a | a :<: a | a :==: a
3849
3850 -- | An empty set of constraints.
3851 empty :: Ord a => Constrs a
3852
3853 -- | Adds one constraint to the set, returns the new set of constraints
3854 --   if consistent.
3855 addConstr :: Ord a => Constr a -> Constrs a -> Maybe (Constrs a)\end{verbatim}
3856 }
3857
3858   \caption{Interface for the module handling the constraints.}
3859   \label{fig:constraint}
3860 \end{figure}
3861
3862
3863 \subsection{Type holes}
3864
3865 When building up programs interactively, it is useful to leave parts
3866 unfinished while exploring the current context.  This is what type holes
3867 are for.
3868
3869 \section{Evaluation}
3870
3871 \section{Future work}
3872
3873 \subsection{Coinduction}
3874
3875 \subsection{Quotient types}
3876
3877 \subsection{Partiality}
3878
3879 \subsection{Pattern matching}
3880
3881 \subsection{Pattern unification}
3882
3883 % TODO coinduction (obscoin, gimenez, jacobs), pattern unification (miller,
3884 % gundry), partiality monad (NAD)
3885
3886 \appendix
3887
3888 \section{Notation and syntax}
3889
3890 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
3891 the type of relation being established and the syntactic elements appearing,
3892 for example
3893
3894 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
3895   Typing derivations here.
3896 }
3897
3898 In the languages presented and Agda code samples I also highlight the syntax,
3899 following a uniform color and font convention:
3900
3901 \begin{center}
3902   \begin{tabular}{c | l}
3903     $\mytyc{Sans}$   & Type constructors. \\
3904     $\mydc{sans}$    & Data constructors. \\
3905     % $\myfld{sans}$  & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
3906     $\mysyn{roman}$  & Keywords of the language. \\
3907     $\myfun{roman}$  & Defined values and destructors. \\
3908     $\myb{math}$     & Bound variables.
3909   \end{tabular}
3910 \end{center}
3911
3912 Moreover, I will from time to time give examples in the Haskell programming
3913 language as defined in \citep{Haskell2010}, which I will typeset in
3914 \texttt{teletype} font.  I assume that the reader is already familiar with
3915 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
3916
3917 When presenting grammars, I will use a word in $\mysynel{math}$ font
3918 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate nonterminals. Additionally,
3919 I will use quite flexibly a $\mysynel{math}$ font to indicate a syntactic
3920 element.  More specifically, terms are usually indicated by lowercase letters
3921 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase letter (often
3922 $\mytya$, $\mytyb$, or $\mytycc$).
3923
3924 When presenting type derivations, I will often abbreviate and present multiple
3925 conclusions, each on a separate line:
3926 \begin{prooftree}
3927   \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
3928   \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
3929   \noLine
3930   \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
3931 \end{prooftree}
3932
3933 I will often present `definitions' in the described calculi and in
3934 $\mykant$\ itself, like so:
3935 \[
3936 \begin{array}{@{}l}
3937   \myfun{name} : \mytysyn \\
3938   \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
3939 \end{array}
3940 \]
3941 To define operators, I use a mixfix notation similar
3942 to Agda, where $\myarg$s denote arguments:
3943 \[
3944 \begin{array}{@{}l}
3945   \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
3946   \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
3947 \end{array}
3948 \]
3949
3950 In explicitly typed systems, I will also omit type annotations when they
3951 are obvious, e.g. by not annotating the type of parameters of
3952 abstractions or of dependent pairs.
3953
3954 I will also introduce multiple arguments in one go in arrow types:
3955 \[
3956   (\myb{x}\, \myb{y} {:} \mytya) \myarr \cdots = (\myb{x} {:} \mytya) \myarr (\myb{y} {:} \mytya) \myarr \cdots
3957 \]
3958 and in abstractions:
3959 \[
3960 \myabs{\myb{x}\myappsp\myb{y}}{\cdots} = \myabs{\myb{x}}{\myabs{\myb{y}}{\cdots}}
3961 \]
3962
3963 \section{Code}
3964
3965 \subsection{ITT renditions}
3966 \label{app:itt-code}
3967
3968 \subsubsection{Agda}
3969 \label{app:agda-itt}
3970
3971 Note that in what follows rules for `base' types are
3972 universe-polymorphic, to reflect the exposition.  Derived definitions,
3973 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
3974 that in the theory presented we don't have universe polymorphism.
3975
3976 \begin{code}
3977 module ITT where
3978   open import Level
3979
3980   data Empty : Set where
3981
3982   absurd : ∀ {a} {A : Set a} → Empty → A
3983   absurd ()
3984
3985   ¬_ : ∀ {a} → (A : Set a) → Set a
3986   ¬ A = A → Empty
3987
3988   record Unit : Set where
3989     constructor tt
3990
3991   record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
3992     constructor _,_
3993     field
3994       fst  : A
3995       snd  : B fst
3996   open _×_ public
3997
3998   data Bool : Set where
3999     true false : Bool
4000
4001   if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
4002   if true / _ then x else _ = x
4003   if false / _ then _ else x = x
4004
4005   if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
4006   if_then_else_ x {P} = if_/_then_else_ x P
4007
4008   data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
4009     _◁_ : (s : S) → (P s → W S P) → W S P
4010
4011   rec : ∀ {a b} {S : Set a} {P : S → Set b}
4012     (C : W S P → Set) →       -- some conclusion we hope holds
4013     ((s : S) →                -- given a shape...
4014      (f : P s → W S P) →      -- ...and a bunch of kids...
4015      ((p : P s) → C (f p)) →  -- ...and C for each kid in the bunch...
4016      C (s ◁ f)) →             -- ...does C hold for the node?
4017     (x : W S P) →             -- If so, ...
4018     C x                       -- ...C always holds.
4019   rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
4020
4021 module Examples-→ where
4022   open ITT
4023
4024   data ℕ : Set where
4025     zero : ℕ
4026     suc : ℕ → ℕ
4027
4028   -- These pragmas are needed so we can use number literals.
4029   {-# BUILTIN NATURAL ℕ #-}
4030   {-# BUILTIN ZERO zero #-}
4031   {-# BUILTIN SUC suc #-}
4032
4033   data List (A : Set) : Set where
4034     [] : List A
4035     _∷_ : A → List A → List A
4036
4037   length : ∀ {A} → List A → ℕ
4038   length [] = zero
4039   length (_ ∷ l) = suc (length l)
4040
4041   _>_ : ℕ → ℕ → Set
4042   zero > _ = Empty
4043   suc _ > zero = Unit
4044   suc x > suc y = x > y
4045
4046   head : ∀ {A} → (l : List A) → length l > 0 → A
4047   head [] p = absurd p
4048   head (x ∷ _) _ = x
4049
4050 module Examples-× where
4051   open ITT
4052   open Examples-→
4053
4054   even : ℕ → Set
4055   even zero = Unit
4056   even (suc zero) = Empty
4057   even (suc (suc n)) = even n
4058
4059   6-even : even 6
4060   6-even = tt
4061
4062   5-not-even : ¬ (even 5)
4063   5-not-even = absurd
4064   
4065   there-is-an-even-number : ℕ × even
4066   there-is-an-even-number = 6 , 6-even
4067
4068   _∨_ : (A B : Set) → Set
4069   A ∨ B = Bool × (λ b → if b then A else B)
4070
4071   left : ∀ {A B} → A → A ∨ B
4072   left x = true , x
4073
4074   right : ∀ {A B} → B → A ∨ B
4075   right x = false , x
4076
4077   [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
4078   [ f , g ] x =
4079     (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
4080
4081 module Examples-W where
4082   open ITT
4083   open Examples-×
4084
4085   Tr : Bool → Set
4086   Tr b = if b then Unit else Empty
4087
4088   ℕ : Set
4089   ℕ = W Bool Tr
4090
4091   zero : ℕ
4092   zero = false ◁ absurd
4093
4094   suc : ℕ → ℕ
4095   suc n = true ◁ (λ _ → n)
4096
4097   plus : ℕ → ℕ → ℕ
4098   plus x y = rec
4099     (λ _ → ℕ)
4100     (λ b →
4101       if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
4102       then (λ _ f → (suc (f tt))) else (λ _ _ → y))
4103     x
4104
4105 module Equality where
4106   open ITT
4107   
4108   data _≡_ {a} {A : Set a} : A → A → Set a where
4109     refl : ∀ x → x ≡ x
4110
4111   ≡-elim : ∀ {a b} {A : Set a}
4112     (P : (x y : A) → x ≡ y → Set b) →
4113     ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
4114   ≡-elim P p (refl x) = p
4115
4116   subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
4117   subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
4118
4119   sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
4120   sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
4121
4122   trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
4123   trans x y z p q = subst (λ z′ → x ≡ z′) q p
4124
4125   cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y 
4126   cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
4127 \end{code}
4128
4129 \subsubsection{\mykant}
4130
4131 The following things are missing: $\mytyc{W}$-types, since our
4132 positivity check is overly strict, and equality, since we haven't
4133 implemented that yet.
4134
4135 {\small
4136 \verbatiminput{itt.ka}
4137 }
4138
4139 \subsection{\mykant\ examples}
4140
4141 {\small
4142 \verbatiminput{examples.ka}
4143 }
4144
4145 \subsection{\mykant's hierachy}
4146 \label{sec:hurkens}
4147
4148 This rendition of the Hurken's paradox does not type check with the
4149 hierachy enabled, type checks and loops without it.  Adapted from an
4150 Agda version, available at
4151 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
4152
4153 {\small
4154 \verbatiminput{hurkens.ka}
4155 }
4156
4157 \bibliographystyle{authordate1}
4158 \bibliography{thesis}
4159
4160 \end{document}