the war is over johnny
[bitonic-mengthesis.git] / thesis.lagda
1 %% I M P O R T A N T
2 %% THIS LATEX HURTS YOUR EYES.  DO NOT READ.
3
4
5 \documentclass[11pt, fleqn, twoside]{article}
6 \usepackage{etex}
7
8 \usepackage[usenames,dvipsnames]{xcolor}
9
10 \usepackage[sc,slantedGreek]{mathpazo}
11 % \linespread{1.05}
12 % \usepackage{times}
13
14 % \oddsidemargin .50in
15 % \evensidemargin -.25in
16 % % \oddsidemargin 0in
17 % % \evensidemargin 0in
18 % \textheight 9.5in 
19 % \textwidth    6.2in
20 % \topmargin    -9mm  
21 % %% \parindent 10pt
22
23 % \headheight 0pt
24 % \headsep 0pt
25
26 \usepackage[hmargin=2cm,vmargin=2.5cm]{geometry}
27 \geometry{textwidth=390pt}
28 \geometry{bindingoffset=1.5cm}
29
30 \raggedbottom
31
32 \usepackage{amsthm}
33
34 %% Bibtex
35 \usepackage{natbib}
36
37 %% Links
38 \usepackage[pdftex, pdfborderstyle={/S/U/W 0}]{hyperref}
39
40 %% Frames
41 \usepackage{framed}
42
43 %% Symbols
44 \usepackage[fleqn]{amsmath}
45 \usepackage{stmaryrd}           %llbracket
46
47 %% Proof trees
48 \usepackage{bussproofs}
49
50 %% Diagrams
51 \usepackage[all]{xy}
52
53 %% Quotations
54 \usepackage{epigraph}
55
56 %% Images
57 \usepackage{graphicx}
58
59 %% Subfigure
60 \usepackage{subcaption}
61
62 \usepackage{verbatim}
63 \usepackage{fancyvrb}
64
65 \RecustomVerbatimEnvironment
66   {Verbatim}{Verbatim}
67   {xleftmargin=9mm}
68
69 %% diagrams
70 \usepackage{tikz}
71 \usetikzlibrary{shapes,arrows,positioning}
72 \usetikzlibrary{intersections}
73 % \usepackage{tikz-cd}
74 % \usepackage{pgfplots}
75
76 \usepackage{titlesec}
77
78 % custom section
79 \titleformat{\section}
80 {\normalfont\huge\scshape}
81 {\thesection\hskip 9pt\textpipe\hskip 9pt}
82 {0pt}
83 {}
84
85 \newcommand{\sectionbreak}{\clearpage}
86
87
88
89 %% -----------------------------------------------------------------------------
90 %% Commands for Agda
91 \usepackage[english]{babel}
92 \usepackage[conor]{agda}
93 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
94 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
95 \renewcommand{\AgdaField}{\AgdaFunction}
96 % \definecolor{AgdaBound} {HTML}{000000}
97 \definecolor{AgdaHole} {HTML} {FFFF33}
98
99 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
100 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
101 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
102 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
103 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
104 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
105
106 \renewenvironment{code}%
107 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed\small}%
108 {\endpboxed\par\noindent%
109 \ignorespacesafterend\small}
110
111
112 %% -----------------------------------------------------------------------------
113 %% Commands
114
115 \newcommand{\mysmall}{}
116 \newcommand{\mysyn}{\AgdaKeyword}
117 \newcommand{\mytyc}[1]{\textup{\AgdaDatatype{#1}}}
118 \newcommand{\mydc}[1]{\textup{\AgdaInductiveConstructor{#1}}}
119 \newcommand{\myfld}[1]{\textup{\AgdaField{#1}}}
120 \newcommand{\myfun}[1]{\textup{\AgdaFunction{#1}}}
121 \newcommand{\myb}[1]{\AgdaBound{$#1$}}
122 \newcommand{\myfield}{\AgdaField}
123 \newcommand{\myind}{\AgdaIndent}
124 \newcommand{\mykant}{\textmd{\textsc{Bertus}}}
125 \newcommand{\mysynel}[1]{#1}
126 \newcommand{\myse}{\mysynel}
127 \newcommand{\mytmsyn}{\mysynel{term}}
128 \newcommand{\mysp}{\ }
129 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
130 \newcommand{\myappsp}{\hspace{0.07cm}}
131 \newcommand{\myapp}[2]{#1 \myappsp #2}
132 \newcommand{\mysynsep}{\ \ |\ \ }
133 \newcommand{\myITE}[3]{\myfun{If}\, #1\, \myfun{Then}\, #2\, \myfun{Else}\, #3}
134 \newcommand{\mycumul}{\preceq}
135
136 \newcommand{\mydesc}[3]{
137   \noindent
138   \mbox{
139     \parbox{\textwidth}{
140       {\mysmall
141         \vspace{0.2cm}
142         \hfill \textup{\phantom{ygp}\textbf{#1}} $#2$
143         \framebox[\textwidth]{
144           \parbox{\textwidth}{
145             \vspace{0.1cm}
146             \centering{
147               #3
148             }
149             \vspace{0.2cm}
150           }
151         }
152         \vspace{0.2cm}
153       }
154     }
155   }
156 }
157
158 \newcommand{\mytmt}{\mysynel{t}}
159 \newcommand{\mytmm}{\mysynel{m}}
160 \newcommand{\mytmn}{\mysynel{n}}
161 \newcommand{\myred}{\leadsto}
162 \newcommand{\mysub}[3]{#1[#3 / #2]}
163 \newcommand{\mytysyn}{\mysynel{type}}
164 \newcommand{\mybasetys}{K}
165 \newcommand{\mybasety}[1]{B_{#1}}
166 \newcommand{\mytya}{\myse{A}}
167 \newcommand{\mytyb}{\myse{B}}
168 \newcommand{\mytycc}{\myse{C}}
169 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
170 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
171 \newcommand{\myctx}{\Gamma}
172 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
173 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
174 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
175 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
176 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
177 \newcommand{\myunit}{\mytyc{Unit}}
178 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
179 \newcommand{\myfst}{\myfld{fst}}
180 \newcommand{\mysnd}{\myfld{snd}}
181 \newcommand{\myconst}{\myse{c}}
182 \newcommand{\myemptyctx}{\varepsilon}
183 \newcommand{\myhole}{\AgdaHole}
184 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
185 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
186 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
187 \newcommand{\myright}[1]{\mydc{right}_{#1}}
188 \newcommand{\myempty}{\mytyc{Empty}}
189 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
190 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
191 \newcommand{\myarg}{\_}
192 \newcommand{\myderivsp}{}
193 \newcommand{\myderivspp}{\vspace{0.3cm}}
194 \newcommand{\mytyp}{\mytyc{Type}}
195 \newcommand{\myneg}{\myfun{$\neg$}}
196 \newcommand{\myar}{\,}
197 \newcommand{\mybool}{\mytyc{Bool}}
198 \newcommand{\mytrue}{\mydc{true}}
199 \newcommand{\myfalse}{\mydc{false}}
200 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
201 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
202 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
203 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
204 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
205 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
206 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
207 \newcommand{\mylist}{\mytyc{List}}
208 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
209 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
210 \newcommand{\myfoldr}{\myfun{foldr}}
211 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
212 \newcommand{\mynodee}{\mathbin{\mydc{$\lhd$}}}
213 \newcommand{\mynode}[2]{\mynodee_{#1.#2}}
214 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
215 \newcommand{\mylub}{\sqcup}
216 \newcommand{\mydefeq}{\cong}
217 \newcommand{\myrefl}{\mydc{refl}}
218 \newcommand{\mypeq}{\mytyc{=}}
219 \newcommand{\myjeqq}{\myfun{$=$-elim}}
220 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
221 \newcommand{\mysubst}{\myfun{subst}}
222 \newcommand{\myprsyn}{\myse{prop}}
223 \newcommand{\myprdec}[1]{\mathopen{\mytyc{$\llbracket$}} #1 \mathclose{\mytyc{$\rrbracket$}}}
224 \newcommand{\myand}{\mathrel{\mytyc{$\wedge$}}}
225 \newcommand{\mybigand}{\mathrel{\mytyc{$\bigwedge$}}}
226 \newcommand{\myprfora}[3]{\forall #1 {:} #2.\, #3}
227 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
228 \newcommand{\mybot}{\mytyc{$\bot$}}
229 \newcommand{\mytop}{\mytyc{$\top$}}
230 \newcommand{\mycoe}{\myfun{coe}}
231 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
232 \newcommand{\mycoh}{\myfun{coh}}
233 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
234 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
235 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
236 \newcommand{\myprop}{\mytyc{Prop}}
237 \newcommand{\mytmup}{\mytmsyn\uparrow}
238 \newcommand{\mydefs}{\Delta}
239 \newcommand{\mynf}{\Downarrow}
240 \newcommand{\myinff}[3]{#1 \vdash #2 \Uparrow #3}
241 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
242 \newcommand{\mychkk}[3]{#1 \vdash #2 \Downarrow #3}
243 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
244 \newcommand{\myann}[2]{#1 : #2}
245 \newcommand{\mydeclsyn}{\myse{decl}}
246 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
247 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
248 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
249 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ \{ #4 \}}
250 \newcommand{\myelabt}{\vdash}
251 \newcommand{\myelabf}{\rhd}
252 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
253 \newcommand{\mytele}{\Delta}
254 \newcommand{\mytelee}{\delta}
255 \newcommand{\mydcctx}{\Gamma}
256 \newcommand{\mynamesyn}{\myse{name}}
257 \newcommand{\myvec}{\overrightarrow}
258 \newcommand{\mymeta}{\textsc}
259 \newcommand{\myhyps}{\mymeta{hyps}}
260 \newcommand{\mycc}{;}
261 \newcommand{\myemptytele}{\varepsilon}
262 \newcommand{\mymetagoes}{\Longrightarrow}
263 % \newcommand{\mytesctx}{\
264 \newcommand{\mytelesyn}{\myse{telescope}}
265 \newcommand{\myrecs}{\mymeta{recs}}
266 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
267 \newcommand{\mylet}{\mysyn{let}}
268 \newcommand{\myhead}{\mymeta{head}}
269 \newcommand{\mytake}{\mymeta{take}}
270 \newcommand{\myix}{\mymeta{ix}}
271 \newcommand{\myapply}{\mymeta{apply}}
272 \newcommand{\mydataty}{\mymeta{datatype}}
273 \newcommand{\myisreco}{\mymeta{record}}
274 \newcommand{\mydcsep}{\ |\ }
275 \newcommand{\mytree}{\mytyc{Tree}}
276 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
277 \newcommand{\mysigma}{\mytyc{$\Sigma$}}
278 \newcommand{\mynegder}{\vspace{-0.3cm}}
279 \newcommand{\myquot}{\Uparrow}
280 \newcommand{\mynquot}{\, \Downarrow}
281 \newcommand{\mycanquot}{\ensuremath{\textsc{quote}{\Downarrow}}}
282 \newcommand{\myneuquot}{\ensuremath{\textsc{quote}{\Uparrow}}}
283 \newcommand{\mymetaguard}{\ |\ }
284 \newcommand{\mybox}{\Box}
285 \newcommand{\mytermi}[1]{\text{\texttt{#1}}}
286 \newcommand{\mysee}[1]{\langle\myse{#1}\rangle}
287
288 \renewcommand{\[}{\begin{equation*}}
289 \renewcommand{\]}{\end{equation*}}
290 \newcommand{\mymacol}[2]{\text{\textcolor{#1}{$#2$}}}
291
292 \newtheorem*{mydef}{Definition}
293 \newtheoremstyle{named}{}{}{\itshape}{}{\bfseries}{}{.5em}{\textsc{#1}}
294 \theoremstyle{named}
295
296 \pgfdeclarelayer{background}
297 \pgfdeclarelayer{foreground}
298 \pgfsetlayers{background,main,foreground}
299
300 \definecolor{webgreen}{rgb}{0,.5,0}
301 \definecolor{webbrown}{rgb}{.6,0,0}
302 \definecolor{webyellow}{rgb}{0.98,0.92,0.73}
303
304 \hypersetup{
305 colorlinks=true, linktocpage=true, pdfstartpage=3, pdfstartview=FitV,
306 breaklinks=true, pdfpagemode=UseNone, pageanchor=true, pdfpagemode=UseOutlines,
307 plainpages=false, bookmarksnumbered, bookmarksopen=true, bookmarksopenlevel=1,
308 hypertexnames=true, pdfhighlight=/O, urlcolor=webbrown, linkcolor=black, citecolor=webgreen}
309
310
311 %% -----------------------------------------------------------------------------
312
313 \title{\mykant: Implementing Observational Equality}
314 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
315 \date{June 2013}
316
317   \iffalse
318   \begin{code}
319     module thesis where
320   \end{code}
321   \fi
322
323 \begin{document}
324
325 \pagenumbering{gobble}
326
327 \begin{center}
328
329
330 % Upper part of the page. The '~' is needed because \\
331 % only works if a paragraph has started.
332 \includegraphics[width=0.4\textwidth]{brouwer-cropped.png}~\\[1cm]
333
334 \textsc{\Large Final year project}\\[0.5cm]
335
336 % Title
337 { \huge \mykant: Implementing Observational Equality}\\[1.5cm]
338
339 {\Large Francesco \textsc{Mazzoli} \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}\\[0.8cm]
340
341   \begin{minipage}{0.4\textwidth}
342   \begin{flushleft} \large
343     \emph{Supervisor:}\\
344     Dr. Steffen \textsc{van Bakel}
345   \end{flushleft}
346 \end{minipage}
347 \begin{minipage}{0.4\textwidth}
348   \begin{flushright} \large
349     \emph{Second marker:} \\
350     Dr. Philippa \textsc{Gardner}
351   \end{flushright}
352 \end{minipage}
353 \vfill
354
355 % Bottom of the page
356 {\large \today}
357
358 \end{center}
359
360 \clearpage
361
362 \mbox{}
363 \clearpage
364
365 \begin{abstract}
366   The marriage between programming and logic has been a fertile one.  In
367   particular, since the definition of the simply typed
368   $\lambda$-calculus, a number of type systems have been devised with
369   increasing expressive power.
370
371   Among this systems, Intuitionistic Type Theory (ITT) has been a
372   popular framework for theorem provers and programming languages.
373   However, reasoning about equality has always been a tricky business in
374   ITT and related theories.  In this thesis we shall explain why this is
375   the case, and present Observational Type Theory (OTT), a solution to
376   some of the problems with equality.
377
378   To bring OTT closer to the current practice of interactive theorem
379   provers, we describe \mykant, a system featuring OTT in a setting more
380   close to the one found in widely used provers such as Agda and Coq.
381   Most notably, we feature used defined inductive and record types and a
382   cumulative, implicit type hierarchy.  Having implemented part of
383   $\mykant$ as a Haskell program, we describe some of the implementation
384   issues faced.
385 \end{abstract}
386
387 \clearpage
388
389 \mbox{}
390 \clearpage
391
392 \renewcommand{\abstractname}{Acknowledgements}
393 \begin{abstract}
394   I would like to thank Steffen van Bakel, my supervisor, who was brave
395   enough to believe in my project and who provided support and
396   invaluable advice.
397
398   I would also like to thank the Haskell and Agda community on
399   \texttt{IRC}, which guided me through the strange world of types; and
400   in particular Andrea Vezzosi and James Deikun, with whom I entertained
401   countless insightful discussions over the past year.  Andrea suggested
402   Observational Type Theory as a topic of study: this thesis would not
403   exist without him.  Before them, Tony Field introduced me to Haskell,
404   unknowingly filling most of my free time from that time on.
405
406   Finally, most of the work stems from the research of Conor McBride,
407   who answered many of my doubts through these months.  I also owe him
408   the colours.
409 \end{abstract}
410
411 \clearpage
412 \mbox{}
413 \clearpage
414
415 \tableofcontents
416
417 \section{Introduction}
418
419 \pagenumbering{arabic}
420
421 Functional programming is in good shape.  In particular the `well-typed'
422 line of work originating from Milner's ML has been extremely fruitful,
423 in various directions.  Nowadays functional, well-typed programming
424 languages like Haskell or OCaml are slowly being absorbed by the
425 mainstream.  An important related development---and in fact the original
426 motivator for ML's existence---is the advancement of the practice of
427 \emph{interactive theorem provers}.
428
429
430 An interactive theorem prover, or proof assistant, is a tool that lets
431 the user develop formal proofs with the confidence of the machine
432 checking them for correctness.  While the effort towards a full
433 formalisation of mathematics has been ongoing for more than a century,
434 theorem provers have been the first class of software whose
435 implementation depends directly on these theories.
436
437 In a fortunate turn of events, it was discovered that well-typed
438 functional programming and proving theorems in an \emph{intuitionistic}
439 logic are the same activity.  Under this discipline, the types in our
440 programming language can be interpreted as proposition in our logic; and
441 the programs implementing the specification given by the types as their
442 proofs.  This fact stimulated an active transfer of techniques and
443 knowledge between logic and programming language theory, in both
444 directions.
445
446 Mathematics could provide programming with a wealth of abstractions and
447 constructs developed over centuries.  Moreover, identifying our types
448 with a logic lets us focus on foundational questions regarding
449 programming with a much more solid approach, given the years of rigorous
450 study of logic.  Programmers, on the other hand, had already developed a
451 number of approaches to effectively collaborate with computers, through
452 the study of programming languages.
453
454 In this space, we shall follow the discipline of Intuitionistic Type
455 Theory, or Martin-L\"{o}f Type Theory, after its inventor.  First
456 formulated in the 70s and then adjusted through a series of revisions,
457 it has endured as the core of many practical systems widely in use
458 today, and it is the most prominent instance of the proposition-as-types
459 and proofs-as-programs paradigm.  One of the most debated subjects in
460 this field has been regarding what notion of equality should be
461 exposed to the user.
462
463 The tension when studying equality in type theory springs from the fact
464 that there is a divide between what the user can prove equal
465 \emph{inside} the theory---what is \emph{propositionally} equal---and
466 what the theorem prover identifies as equal in its meta-theory---what is
467 \emph{definitionally} equal.  If we want our system to be well behaved
468 (mostly if we want to keep type checking decidable) we must keep the two
469 notions separate, with definitional equality inducing propositional
470 equality, but not the reverse.  However in this scenario propositional
471 equality is weaker than we would like: we can only prove terms equal
472 based on their syntactical structure, and not based on their behaviour.
473
474 This thesis is concerned with exploring a new approach in this area,
475 \emph{observational} equality.  Promising to provide a more adequate
476 propositional equality while retaining well-behavedness, it still is a
477 relatively unexplored notion.  We set ourselves to change that by
478 studying it in a setting more akin to the one found in currently
479 available theorem provers.
480
481 \subsection{Structure}
482
483 Section \ref{sec:types} will give a brief overview of the
484 $\lambda$-calculus, both typed and untyped.  This will give us the
485 chance to introduce most of the concepts mentioned above rigorously, and
486 gain some intuition about them.  An excellent introduction to types in
487 general can be found in \cite{Pierce2002}, although not from the
488 perspective of theorem proving.
489
490 Section \ref{sec:itt} will describe a set of basic construct that form a
491 `baseline' Intuitionistic Type Theory.  The goal is to familiarise with
492 the main concept of ITT before attacking the problem of equality.  Given
493 the wealth of material covered the exposition is quite dense.  Good
494 introductions can be found in \cite{Thompson1991}, \cite{Nordstrom1990},
495 and \cite{Martin-Lof1984} himself.
496
497 Section \ref{sec:equality} will introduce propositional equality.  The
498 properties of propositional equality will be discussed along with its
499 limitations.  After reviewing some extensions to propositional equality,
500 we will explain why identifying definitional equality with propositional
501 equality causes problems.
502
503 Section \ref{sec:ott} will introduce observational equality, following
504 closely the original exposition by \cite{Altenkirch2007}.  The
505 presentation is free-standing but glosses over the meta-theoretic
506 properties of OTT, focusing on the mechanisms that make it work.
507
508 Section \ref{sec:kant-theory} is the central part of the thesis and will
509 describe \mykant, a system we have developed incorporating OTT along
510 constructs usually present in modern theorem provers.  Along the way, we
511 discuss these additional features and their trade-offs.  Section
512 \ref{sec:kant-practice} will describe an implementation implementing
513 part of \mykant.  A high level design of the software is given, along
514 with a few specific implementation issues.
515
516 Finally, Section \ref{sec:evaluation} will asses the decisions made in
517 designing and implementing \mykant and the results achieved; and Section
518 \ref{sec:future-work} will give a roadmap to bring \mykant\ on par and
519 beyond the competition.
520
521 \subsection{Contributions}
522 \label{sec:contributions}
523
524 The contribution of this thesis is threefold:
525
526 \begin{itemize}
527 \item Provide a description of observational equality `in context', to
528   make the subject more accessible.  Considering the possibilities that
529   OTT brings to the table, we think that introducing it to a wider
530   audience can only be beneficial.
531
532 \item Fill in the gaps needed to make OTT work with user-defined
533   inductive types and a type hierarchy.  We show how one notion of
534   equality is enough, instead of separate notions of value- and
535   type-equality as presented in the original paper.  We are able to keep
536   the type equalities `small' while preserving subject reduction by
537   exploiting the fact that we work within a cumulative theory.
538   Incidentally, we also describe a generalised version of bidirectional
539   type checking for user defined types.
540
541 \item Provide an implementation to probe the possibilities of OTT in a
542   more realistic setting.  We have implemented an ITT with user defined
543   types but due to the limited time constraints we were not able to
544   complete the implementation of observational equality.  Nonetheless,
545   we describe some interesting implementation issues faced by the type
546   theory implementor.
547 \end{itemize}
548
549 The system developed as part of this thesis, \mykant, incorporates OTT
550 with features that are familiar to users of existing theorem provers
551 adopting the proofs-as-programs mantra.  The defining features of
552 \mykant\ are:
553
554 \begin{description}
555 \item[Full dependent types] In ITT, types are very `first class' notion
556   and can be the result of computation---they can \emph{depend} on
557   values, thus the name \emph{dependent types}.  \mykant\ espouses this
558   notion to its full consequences.
559
560 \item[User defined data types and records] Instead of forcing the user
561   to choose from a restricted toolbox, we let her define types for
562   greater flexibility.  We have two kinds of user defined types:
563   inductive data types, formed by various data constructors whose type
564   signatures can contain recursive occurrences of the type being
565   defined; and records, where we have just one data constructor and
566   projections to extract each each field in said constructor.
567
568 \item[Consistency] Our system is meant to be consistent with respects to
569   the logic it embodies.  For this reason, we restrict recursion to
570   \emph{structural} recursion on the defined inductive types, through
571   the use of operators (destructors) computing on each type.  Following
572   the types-as-propositions interpretation, each destructor expresses an
573   induction principle on the data type it operates on.  To achieve the
574   consistency of these operations we make sure that our recursive data
575   types are \emph{strictly positive}.
576
577 \item[Bidirectional type checking] We take advantage of a
578   \emph{bidirectional} type inference system in the style of
579   \cite{Pierce2000}.  This cuts down the type annotations by a
580   considerable amount in an elegant way and at a very low cost.
581   Bidirectional type checking is usually employed in core calculi, but
582   in \mykant\ we extend the concept to user defined data types.
583
584 \item[Type hierarchy] In set theory we have to take treat powerset-like
585   objects with care, if we want to avoid paradoxes.  However, the
586   working mathematician is rarely concerned by this, and the consistency
587   in this regard is implicitly assumed.  In the tradition of
588   \cite{Russell1927}, in \mykant\ we employ a \emph{type hierarchy} to
589   make sure that these size issues are taken care of; and we employ
590   system so that the user will be free from thinking about the
591   hierarchy, just like the mathematician is.
592
593 \item[Observational equality] The motivator of this thesis, \mykant\
594   incorporates a notion of observational equality, modifying the
595   original presentation by \cite{Altenkirch2007} to fit our more
596   expressive system.  As mentioned, we reconcile OTT with user defined
597   types and a type hierarchy. 
598
599 \item[Type holes] When building up programs interactively, it is useful
600   to leave parts unfinished while exploring the current context.  This
601   is what type holes are for.
602 \end{description}
603
604 \subsection{Notation and syntax}
605
606 Appendix \ref{app:notation} describes the notation and syntax used in
607 this thesis.
608
609 \section{Simple and not-so-simple types}
610 \label{sec:types}
611
612 \epigraph{\emph{Well typed programs can't go wrong.}}{Robin Milner}
613
614 \subsection{The untyped $\lambda$-calculus}
615 \label{sec:untyped}
616
617 Along with Turing's machines, the earliest attempts to formalise
618 computation lead to the definition of the $\lambda$-calculus
619 \citep{Church1936}.  This early programming language encodes computation
620 with a minimal syntax and no `data' in the traditional sense, but just
621 functions.  Here we give a brief overview of the language, which will
622 give the chance to introduce concepts central to the analysis of all the
623 following calculi.  The exposition follows the one found in Chapter 5 of
624 \cite{Queinnec2003}.
625
626 \begin{mydef}[$\lambda$-terms]
627   Syntax of the $\lambda$-calculus: variables, abstractions, and
628   applications.
629 \end{mydef}
630 \mynegder
631 \mydesc{syntax}{ }{
632   $
633   \begin{array}{r@{\ }c@{\ }l}
634     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
635     x          & \in & \text{Some enumerable set of symbols}
636   \end{array}
637   $
638 }
639
640 Parenthesis will be omitted in the usual way, with application being
641 left associative.
642
643 Abstractions roughly corresponds to functions, and their semantics is more
644 formally explained by the $\beta$-reduction rule.
645
646 \begin{mydef}[$\beta$-reduction]
647 $\beta$-reduction and substitution for the $\lambda$-calculus.
648 \end{mydef}
649 \mynegder
650 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
651   $
652   \begin{array}{l}
653     \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{ \textbf{where}} \\
654     \myind{2}
655     \begin{array}{l@{\ }c@{\ }l}
656       \mysub{\myb{y}}{\myb{x}}{\mytmn} \mymetaguard \myb{x} = \myb{y} & \mymetagoes & \mytmn \\
657       \mysub{\myb{y}}{\myb{x}}{\mytmn} & \mymetagoes & \myb{y} \\
658       \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
659       \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & \myabs{\myb{x}}{\mytmm} \\
660       \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}} \\
661       \multicolumn{3}{l}{\myind{2} \text{\textbf{with} $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
662     \end{array}
663   \end{array}
664   $
665 }
666
667 The care required during substituting variables for terms is to avoid
668 name capturing.  We will use substitution in the future for other
669 name-binding constructs assuming similar precautions.
670
671 These few elements have a remarkable expressiveness, and are in fact
672 Turing complete.  As a corollary, we must be able to devise a term that
673 reduces forever (`loops' in imperative terms):
674 \[
675   (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, \textbf{where} $\omega = \myabs{x}{\myapp{x}{x}}$}
676 \]
677 \begin{mydef}[redex]
678   A \emph{redex} is a term that can be reduced.
679 \end{mydef}
680 In the untyped $\lambda$-calculus this will be the case for an
681 application in which the first term is an abstraction, but in general we
682 call a term reducible if it appears to the left of a reduction rule.
683 \begin{mydef}[normal form]
684   A term that contains no redexes is said to be in \emph{normal form}.
685 \end{mydef}
686 \begin{mydef}[normalising terms and systems]
687   Terms that reduce in a finite number of reduction steps to a normal
688   form are \emph{normalising}.  A system in which all terms are
689   normalising is said to have the \emph{normalisation property}, or
690   to be \emph{normalising}.
691 \end{mydef}
692 Given the reduction behaviour of $(\myapp{\omega}{\omega})$, it is clear
693 that the untyped $\lambda$-calculus does not have the normalisation
694 property.
695
696 We have not presented reduction in an algorithmic way, but
697 \emph{evaluation strategies} can be employed to reduce term
698 systematically. Common evaluation strategies include \emph{call by
699   value} (or \emph{strict}), where arguments of abstractions are reduced
700 before being applied to the abstraction; and conversely \emph{call by
701   name} (or \emph{lazy}), where we reduce only when we need to do so to
702 proceed---in other words when we have an application where the function
703 is still not a $\lambda$. In both these strategies we never
704 reduce under an abstraction.  For this reason a weaker form of
705 normalisation is used, where all abstractions are said to be in
706 \emph{weak head normal form} even if their body is not.
707
708 \subsection{The simply typed $\lambda$-calculus}
709
710 A convenient way to `discipline' and reason about $\lambda$-terms is to
711 assign \emph{types} to them, and then check that the terms that we are
712 forming make sense given our typing rules \citep{Curry1934}.  The first
713 most basic instance of this idea takes the name of \emph{simply typed
714   $\lambda$-calculus} (STLC).
715 \begin{mydef}[Simply typed $\lambda$-calculus]
716   The syntax and typing rules for the STLC are given in Figure \ref{fig:stlc}.
717 \end{mydef}
718
719 Our types contain a set of \emph{type variables} $\Phi$, which might
720 correspond to some `primitive' types; and $\myarr$, the type former for
721 `arrow' types, the types of functions.  The language is explicitly
722 typed: when we bring a variable into scope with an abstraction, we
723 declare its type.  Reduction is unchanged from the untyped
724 $\lambda$-calculus.
725
726 \begin{figure}[t]
727   \mydesc{syntax}{ }{
728     $
729     \begin{array}{r@{\ }c@{\ }l}
730       \mytmsyn   & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
731       (\myapp{\mytmsyn}{\mytmsyn}) \\
732       \mytysyn   & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn  \mysynsep \\
733       \myb{x}    & \in & \text{Some enumerable set of symbols} \\
734       \myse{\phi} & \in & \Phi
735     \end{array}
736     $
737   }
738   
739   \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
740       \begin{tabular}{ccc}
741         \AxiomC{$\myctx(x) = A$}
742         \UnaryInfC{$\myjud{\myb{x}}{A}$}
743         \DisplayProof
744         &
745         \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
746         \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
747         \DisplayProof
748         &
749         \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
750         \AxiomC{$\myjud{\mytmn}{\mytya}$}
751         \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
752         \DisplayProof
753       \end{tabular}
754 }
755   \caption{Syntax and typing rules for the STLC.  Reduction is unchanged from
756     the untyped $\lambda$-calculus.}
757   \label{fig:stlc}
758 \end{figure}
759
760 In the typing rules, a context $\myctx$ is used to store the types of
761 bound variables: $\myemptyctx$ is the empty context, and $\myctx;
762 \myb{x} : \mytya$ adds a variable to the context.  $\myctx(x)$ extracts
763 the type of the rightmost occurrence of $x$.
764
765 This typing system takes the name of `simply typed lambda calculus' (STLC), and
766 enjoys a number of properties.  Two of them are expected in most type systems
767 \citep{Pierce2002}:
768 \begin{mydef}[Progress]
769   A well-typed term is not stuck---it is either a variable, or it does
770   not appear on the left of the $\myred$ relation , or it can take a
771   step according to the evaluation rules.
772 \end{mydef}
773 \begin{mydef}[Subject reduction]
774   If a well-typed term takes a step of evaluation, then the
775   resulting term is also well-typed, and preserves the previous type.
776 \end{mydef}
777
778 However, STLC buys us much more: every well-typed term is normalising
779 \citep{Tait1967}.  It is easy to see that we cannot fill the blanks if we want to
780 give types to the non-normalising term shown before:
781 \[
782   \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
783 \]
784 This makes the STLC Turing incomplete.  We can recover the ability to loop by
785 adding a combinator that recurses:
786 \begin{mydef}[Fixed-point combinator]\end{mydef}
787 \mynegder
788 \noindent
789 \begin{minipage}{0.5\textwidth}
790 \mydesc{syntax}{ } {
791   $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
792   \vspace{0.5cm}
793 }
794 \end{minipage} 
795 \begin{minipage}{0.5\textwidth}
796 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
797     \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
798     \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
799     \DisplayProof
800 }
801 \end{minipage} 
802 \mynegder
803 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
804     $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
805 }
806
807 This will deprive us of normalisation, which is a particularly bad thing if we
808 want to use the STLC as described in the next section.
809
810 Another important property of the STLC is the Church-Rosser property:
811 \begin{mydef}[Church-Rosser property]
812   A system is said to have the \emph{Church-Rosser} property, or to be
813   \emph{confluent}, if given any two reductions $\mytmm$ and $\mytmn$ of
814   a given term $\mytmt$, there is exist a term to which both $\mytmm$
815   and $\mytmn$ can be reduced.
816 \end{mydef}
817 Given that the STLC has the normalisation property and the Church-Rosser
818 property, each term has a \emph{unique} normal form.
819
820 \subsection{The Curry-Howard correspondence}
821
822 As hinted in the introduction, it turns out that the STLC can be seen a
823 natural deduction system for intuitionistic propositional logic.  Terms
824 correspond to proofs, and their types correspond to the propositions
825 they prove.  This remarkable fact is known as the Curry-Howard
826 correspondence, or isomorphism.
827
828 The arrow ($\myarr$) type corresponds to implication.  If we wish to prove that
829 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
830 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
831 correct type:
832 \[
833   \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
834 \]
835 Which is known to functional programmers as function composition. Going
836 beyond arrow types, we can extend our bare lambda calculus with useful
837 types to represent other logical constructs.
838 \begin{mydef}[The extended STLC]
839   Figure \ref{fig:natded} shows syntax, reduction, and typing rules for
840   the \emph{extended simply typed $\lambda$-calculus}.
841 \end{mydef}
842
843 \begin{figure}[t]
844 \mydesc{syntax}{ }{
845   $
846   \begin{array}{r@{\ }c@{\ }l}
847     \mytmsyn & ::= & \cdots \\
848              &  |  & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
849              &  |  & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
850                      \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
851                      \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
852              &  |  & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
853                      \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
854     \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
855   \end{array}
856   $
857 }
858
859 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
860     \begin{tabular}{cc}
861       $
862       \begin{array}{l@{ }l@{\ }c@{\ }l}
863         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
864           \myapp{\mytmm}{\mytmt} \\
865         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
866           \myapp{\mytmn}{\mytmt}
867       \end{array}
868       $
869       &
870       $
871       \begin{array}{l@{ }l@{\ }c@{\ }l}
872         \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
873         \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
874       \end{array}
875       $
876     \end{tabular}
877 }
878
879 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
880     \begin{tabular}{cc}
881       \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
882       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
883       \DisplayProof
884       &
885       \AxiomC{$\myjud{\mytmt}{\myempty}$}
886       \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
887       \DisplayProof
888     \end{tabular}
889
890   \myderivspp
891
892     \begin{tabular}{cc}
893       \AxiomC{$\myjud{\mytmt}{\mytya}$}
894       \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
895       \DisplayProof
896       &
897       \AxiomC{$\myjud{\mytmt}{\mytyb}$}
898       \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
899       \DisplayProof
900
901     \end{tabular}
902
903   \myderivspp
904
905     \begin{tabular}{cc}
906       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
907       \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
908       \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
909       \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
910       \DisplayProof
911     \end{tabular}
912
913   \myderivspp
914
915     \begin{tabular}{ccc}
916       \AxiomC{$\myjud{\mytmm}{\mytya}$}
917       \AxiomC{$\myjud{\mytmn}{\mytyb}$}
918       \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
919       \DisplayProof
920       &
921       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
922       \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
923       \DisplayProof
924       &
925       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
926       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
927       \DisplayProof
928     \end{tabular}
929 }
930 \caption{Rules for the extended STLC.  Only the new features are shown, all the
931   rules and syntax for the STLC apply here too.}
932   \label{fig:natded}
933 \end{figure}
934
935 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
936 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
937 products---$\myprod$ here, tuples in Haskell) correspond to
938 conjunctions.  This is apparent looking at the ways to construct and
939 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
940 and $\myright{ }$ correspond to $\vee$ introduction, and
941 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
942 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
943 and $\mysnd$ to $\wedge$ elimination.
944
945 The trivial type $\myunit$ corresponds to the logical $\top$ (true), and
946 dually $\myempty$ corresponds to the logical $\bot$ (false).  $\myunit$
947 has one introduction rule ($\mytt$), and thus one inhabitant; and no
948 eliminators---we cannot gain any information from a witness of the
949 single member of $\myunit$.  $\myempty$ has no introduction rules, and
950 thus no inhabitants; and one eliminator ($\myabsurd{ }$), corresponding
951 to the logical \emph{ex falso quodlibet}.
952
953 With these rules, our STLC now looks remarkably similar in power and use to the
954 natural deduction we already know.
955 \begin{mydef}[Negation]
956   $\myneg \mytya$ can be expressed as $\mytya \myarr \myempty$.
957 \end{mydef}
958 However, there is an important omission: there is no term of
959 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
960 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
961 a type equivalent to those.
962
963 This has a considerable effect on our logic and it is no coincidence, since there
964 is no obvious computational behaviour for laws like the excluded middle.
965 Logics of this kind are called \emph{intuitionistic}, or \emph{constructive},
966 and all the systems analysed will have this characteristic since they build on
967 the foundation of the STLC.\footnote{There is research to give computational
968   behaviour to classical logic, but I will not touch those subjects.}
969
970 As in logic, if we want to keep our system consistent, we must make sure that no
971 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
972 The variant of STLC presented here is indeed
973 consistent, a result that follows from the fact that it is
974 normalising.
975 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
976 desire for consistency.  The following term works for every type $\mytya$,
977 including bottom:
978 \[(\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya\]
979
980 \subsection{Inductive data}
981 \label{sec:ind-data}
982
983 To make the STLC more useful as a programming language or reasoning tool it is
984 common to include (or let the user define) inductive data types.  These comprise
985 of a type former, various constructors, and an eliminator (or destructor) that
986 serves as primitive recursor.
987
988 \begin{mydef}[Finite lists for the STLC]
989 We add a $\mylist$ type constructor, along with an `empty
990 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor.  The eliminator for
991 lists will be the usual folding operation ($\myfoldr$).  Full rules in Figure
992 \ref{fig:list}.
993 \end{mydef}
994 \mynegder
995 \begin{figure}[h]
996 \mydesc{syntax}{ }{
997   $
998   \begin{array}{r@{\ }c@{\ }l}
999     \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
1000                      \mysynsep
1001                      \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
1002     \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
1003   \end{array}
1004   $
1005 }
1006 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1007   $
1008   \begin{array}{l@{\ }c@{\ }l}
1009     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
1010
1011     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
1012     \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
1013   \end{array}
1014   $
1015 }
1016 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
1017     \begin{tabular}{cc}
1018       \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
1019       \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
1020       \DisplayProof
1021       &
1022       \AxiomC{$\myjud{\mytmm}{\mytya}$}
1023       \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
1024       \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
1025       \DisplayProof
1026     \end{tabular}
1027   \myderivspp
1028
1029     \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
1030     \AxiomC{$\myjud{\mytmm}{\mytyb}$}
1031     \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
1032     \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
1033     \DisplayProof
1034 }
1035 \caption{Rules for lists in the STLC.}
1036 \label{fig:list}
1037 \end{figure}
1038
1039 In Section \ref{sec:well-order} we will see how to give a general account of
1040 inductive data.
1041
1042 \section{Intuitionistic Type Theory}
1043 \label{sec:itt}
1044
1045 \epigraph{\emph{Martin-L{\"o}f's type theory is a well established and
1046     convenient arena in which computational Christians are regularly
1047     fed to logical lions.}}{Conor McBride}
1048
1049 \subsection{Extending the STLC}
1050
1051 \cite{Barendregt1991} succinctly expressed geometrically how we can add
1052 expressively to the STLC:
1053 $$
1054 \xymatrix@!0@=1.5cm{
1055   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
1056   & & \lambda C \ar@{-}[dd]
1057   \\
1058   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
1059   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
1060   \\
1061   & \lambda\underline\omega \ar@{-}'[r][rr]
1062   & & \lambda P\underline\omega
1063   \\
1064   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
1065   & & \lambda P \ar@{-}[ur]
1066 }
1067 $$
1068 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
1069 3 dimensions:
1070 \begin{description}
1071 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
1072   types in our type signatures.  For example, we can define a polymorphic
1073   identity function, where $\mytyp$ denotes the `type of types':
1074   \[\displaystyle
1075   (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} {:} \mytyp) \myarr \myb{A} \myarr \myb{A}
1076   \]
1077   The first and most famous instance of this idea has been System F.
1078   This form of polymorphism and has been wildly successful, also thanks
1079   to a well known inference algorithm for a restricted version of System
1080   F known as Hindley-Milner \citep{milner1978theory}.  Languages like
1081   Haskell and SML are based on this discipline.  In Haskell the above
1082   example would be
1083   \begin{Verbatim}
1084 id :: a -> a
1085 id x = x
1086   \end{Verbatim}
1087   Where \texttt{a} implicitly quantifies over a type, and will be
1088   instantiated automatically thanks to the inference.
1089 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
1090   type operators.  For example we could define a function that given types $R$
1091   and $\mytya$ forms the type that represents a value of type $\mytya$ in
1092   continuation passing style:
1093   \[\displaystyle(\myabss{\myb{R} \myarr \myb{A}}{\mytyp}{(\myb{A}
1094     \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp
1095   \]
1096   In Haskell we can define type operator of sorts, although we must
1097   pair them with data constructors, to keep inference manageable:
1098   \begin{Verbatim}
1099 newtype Cont r a = Cont ((a -> r) -> r)
1100   \end{Verbatim}
1101   Where the `type' (kind in Haskell parlance) of \texttt{Cont} will be
1102   \texttt{* -> * -> *}, with \texttt{*} signifying the type of types in
1103   Haskell.
1104 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
1105   types', give great expressive power.  For example, we can have values of whose
1106   type depend on a boolean:
1107   \[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
1108   \myarr \mytyp\] We cannot give an Haskell example that expresses this
1109   concept since Haskell does not support dependent types---it would be a
1110   very different language if it did.
1111 \end{description}
1112
1113 All the systems preserve the properties that make the STLC well behaved.  The
1114 system we are going to focus on, Intuitionistic Type Theory, has all of the
1115 above additions, and thus would sit where $\lambda{C}$ sits in the
1116 `$\lambda$-cube'.  It will serve as the logical `core' of all the other
1117 extensions that we will present and ultimately our implementation of a similar
1118 logic.
1119
1120 \subsection{A Bit of History}
1121
1122 Logic frameworks and programming languages based on type theory have a
1123 long history.  Per Martin-L\"{o}f described the first version of his
1124 theory in 1971, but then revised it since the original version was
1125 inconsistent due to its impredicativity.\footnote{In the early version
1126   there was only one universe $\mytyp$ and $\mytyp : \mytyp$; see
1127   Section \ref{sec:term-types} for an explanation on why this causes
1128   problems.}  For this reason he later gave a revised and consistent
1129 definition \citep{Martin-Lof1984}.
1130
1131 A related development is the polymorphic $\lambda$-calculus, and specifically
1132 the previously mentioned System F, which was developed independently by Girard
1133 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
1134 fact is that while System F is impredicative it is still consistent and strongly
1135 normalising.  \cite{Coquand1986} further extended this line of work with the
1136 Calculus of Constructions (CoC).
1137
1138 Most widely used interactive theorem provers are based on ITT.  Popular
1139 ones include Agda \citep{Norell2007}, Coq \citep{Coq}, Epigram
1140 \citep{McBride2004, EpigramTut}, Isabelle \citep{Paulson1990}, and many
1141 others.
1142
1143 \subsection{A simple type theory}
1144 \label{sec:core-tt}
1145
1146 The calculus I present follows the exposition in \cite{Thompson1991},
1147 and is quite close to the original formulation of \cite{Martin-Lof1984}.
1148 Agda and \mykant\ renditions of the presented theory and all the
1149 examples (even the ones presented only as type signatures) are
1150 reproduced in Appendix \ref{app:itt-code}.
1151 \begin{mydef}[Intuitionistic Type Theory (ITT)]
1152 The syntax and reduction rules are shown in Figure \ref{fig:core-tt-syn}.
1153 The typing rules are presented piece by piece in the following sections.
1154 \end{mydef}
1155
1156 \begin{figure}[t]
1157 \mydesc{syntax}{ }{
1158   $
1159   \begin{array}{r@{\ }c@{\ }l}
1160     \mytmsyn & ::= & \myb{x} \mysynsep
1161                      \mytyp_{level} \mysynsep
1162                      \myunit \mysynsep \mytt \mysynsep
1163                      \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
1164              &  |  & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
1165                      \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1166              &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1167                      \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1168                      (\myapp{\mytmsyn}{\mytmsyn}) \\
1169              &  |  & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1170                      \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
1171              &  |  & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
1172              &  |  & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1173                      \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
1174              &  |  & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
1175     level    & \in & \mathbb{N}
1176   \end{array}
1177   $
1178 }
1179
1180 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1181     \begin{tabular}{ccc}
1182       $
1183       \begin{array}{l@{ }l@{\ }c@{\ }l}
1184         \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
1185         \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
1186       \end{array}
1187       $
1188       &
1189       $
1190       \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
1191       $
1192       &
1193     $
1194     \begin{array}{l@{ }l@{\ }c@{\ }l}
1195       \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
1196       \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
1197     \end{array}
1198     $
1199     \end{tabular}
1200
1201     \myderivspp
1202
1203     $
1204     \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
1205     \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
1206       \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
1207     })}
1208     $
1209 }
1210 \caption{Syntax and reduction rules for our type theory.}
1211 \label{fig:core-tt-syn}
1212 \end{figure}
1213
1214 \subsubsection{Types are terms, some terms are types}
1215 \label{sec:term-types}
1216
1217 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1218     \begin{tabular}{cc}
1219       \AxiomC{$\myjud{\mytmt}{\mytya}$}
1220       \AxiomC{$\mytya \mydefeq \mytyb$}
1221       \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
1222       \DisplayProof
1223       &
1224       \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
1225       \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
1226       \DisplayProof
1227     \end{tabular}
1228 }
1229
1230 The first thing to notice is that a barrier between values and types that we had
1231 in the STLC is gone: values can appear in types, and the two are treated
1232 uniformly in the syntax.
1233
1234 While the usefulness of doing this will become clear soon, a consequence is
1235 that since types can be the result of computation, deciding type equality is
1236 not immediate as in the STLC.
1237 \begin{mydef}[Definitional equality]
1238   We define \emph{definitional
1239   equality}, $\mydefeq$, as the congruence relation extending
1240 $\myred$.  Moreover, when comparing types syntactically we do it up to
1241 renaming of bound names ($\alpha$-renaming)
1242 \end{mydef}
1243 For example under this discipline we will find that
1244 \[
1245 \begin{array}{@{}l}
1246   \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}} \\
1247   \myapp{(\myabss{\myb{f}}{\mytya \myarr \mytya}{\myb{f}})}{(\myabss{\myb{y}}{\mytya}{\myb{y}})} \mydefeq \myabss{\myb{quux}}{\mytya}{\myb{quux}}
1248 \end{array}
1249 \]
1250 Types that are definitionally equal can be used interchangeably.  Here
1251 the `conversion' rule is not syntax directed, but it is possible to
1252 employ $\myred$ to decide term equality in a systematic way, comparing
1253 terms by reducing to their normal forms and then comparing them
1254 syntactically; so that a separate conversion rule is not needed.
1255 Another thing to notice is that, considering the need to reduce terms to
1256 decide equality, for type checking to be decidable a dependently typed
1257 must be terminating and confluent; since every type needs to have a
1258 unique normal form for definitional equality to be decidable.
1259
1260 Moreover, we specify a \emph{type hierarchy} to talk about `large'
1261 types: $\mytyp_0$ will be the type of types inhabited by data:
1262 $\mybool$, $\mynat$, $\mylist$, etc.  $\mytyp_1$ will be the type of
1263 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
1264 \mytyp_0 : \mytyp_1 : \cdots$.  Each type `level' is often called a
1265 universe in the literature.  While it is possible to simplify things by
1266 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
1267 inconsistent for much the same reason that impredicative na\"{\i}ve set
1268 theory is \citep{Hurkens1995}.  However various techniques can be
1269 employed to lift the burden of explicitly handling universes, as we will
1270 see in Section \ref{sec:term-hierarchy}.
1271
1272 \subsubsection{Contexts}
1273
1274 \begin{minipage}{0.5\textwidth}
1275   \mydesc{context validity:}{\myvalid{\myctx}}{
1276       \begin{tabular}{cc}
1277         \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1278         \UnaryInfC{$\myvalid{\myemptyctx}$}
1279         \DisplayProof
1280         &
1281         \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1282         \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
1283         \DisplayProof
1284       \end{tabular}
1285   }
1286 \end{minipage} 
1287 \begin{minipage}{0.5\textwidth}
1288   \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1289       \AxiomC{$\myctx(x) = \mytya$}
1290       \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
1291       \DisplayProof
1292   }
1293 \end{minipage}
1294 \vspace{0.1cm}
1295
1296 We need to refine the notion context to make sure that every variable appearing
1297 is typed correctly, or that in other words each type appearing in the context is
1298 indeed a type and not a value.  In every other rule, if no premises are present,
1299 we assume the context in the conclusion to be valid.
1300
1301 Then we can re-introduce the old rule to get the type of a variable for a
1302 context.
1303
1304 \subsubsection{$\myunit$, $\myempty$}
1305
1306 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1307     \begin{tabular}{ccc}
1308       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1309       \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
1310       \noLine
1311       \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
1312       \DisplayProof
1313       &
1314       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1315       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
1316       \noLine
1317       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
1318       \DisplayProof
1319       &
1320       \AxiomC{$\myjud{\mytmt}{\myempty}$}
1321       \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1322       \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
1323       \noLine
1324       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
1325       \DisplayProof
1326     \end{tabular}
1327 }
1328
1329 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
1330 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
1331 sure that we are invoking $\myabsurd{}$ over a type.
1332
1333 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
1334
1335 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1336    \begin{tabular}{ccc}
1337      \AxiomC{}
1338      \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
1339      \DisplayProof
1340      &
1341      \AxiomC{}
1342      \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
1343      \DisplayProof
1344      &
1345      \AxiomC{}
1346       \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
1347       \DisplayProof
1348     \end{tabular}
1349     \myderivspp
1350
1351     \AxiomC{$\myjud{\mytmt}{\mybool}$}
1352     \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
1353     \noLine
1354     \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
1355     \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
1356     \DisplayProof
1357 }
1358
1359 With booleans we get the first taste of the `dependent' in `dependent
1360 types'.  While the two introduction rules for $\mytrue$ and $\myfalse$
1361 are not surprising, the typing rules for $\myfun{if}$ are.  In most
1362 strongly typed languages we expect the branches of an $\myfun{if}$
1363 statements to be of the same type, to preserve subject reduction, since
1364 execution could take both paths.  This is a pity, since the type system
1365 does not reflect the fact that in each branch we gain knowledge on the
1366 term we are branching on.  Which means that programs along the lines of
1367 \begin{Verbatim}
1368 if null xs then head xs else 0
1369 \end{Verbatim}
1370 are a necessary, well-typed, danger.
1371
1372 However, in a more expressive system, we can do better: the branches' type can
1373 depend on the value of the scrutinised boolean.  This is what the typing rule
1374 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1375 representing the scrutinised boolean type, and the branches are type checked with
1376 the updated knowledge of the value of $\myb{x}$.
1377
1378 \subsubsection{$\myarr$, or dependent function}
1379 \label{sec:depprod}
1380
1381  \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1382      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1383      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1384      \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1385      \DisplayProof
1386
1387      \myderivspp
1388
1389     \begin{tabular}{cc}
1390       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1391       \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1392       \DisplayProof
1393       &
1394       \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1395       \AxiomC{$\myjud{\mytmn}{\mytya}$}
1396       \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1397       \DisplayProof
1398     \end{tabular}
1399 }
1400
1401 Dependent functions are one of the two key features that characterise
1402 dependent types---the other being dependent products.  With dependent
1403 functions, the result type can depend on the value of the argument.
1404 This feature, together with the fact that the result type might be a
1405 type itself, brings a lot of interesting possibilities.  In the
1406 introduction rule, the return type is type checked in a context with an
1407 abstracted variable of domain's type; and in the elimination rule the
1408 actual argument is substituted in the return type.  Keeping the
1409 correspondence with logic alive, dependent functions are much like
1410 universal quantifiers ($\forall$) in logic.
1411
1412 For example, assuming that we have lists and natural numbers in our
1413 language, using dependent functions we can write functions of
1414 types
1415 \[
1416 \begin{array}{l}
1417 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1418 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1419 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1420                \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{$>$}} 0 \myarr
1421                \myb{A}
1422 \end{array}
1423 \]
1424
1425 \myfun{length} is the usual polymorphic length
1426 function. $\myarg\myfun{$>$}\myarg$ is a function that takes two naturals
1427 and returns a type: if the lhs is greater then the rhs, $\myunit$ is
1428 returned, $\myempty$ otherwise.  This way, we can express a
1429 `non-emptiness' condition in $\myfun{head}$, by including a proof that
1430 the length of the list argument is non-zero.  This allows us to rule out
1431 the `empty list' case, so that we can safely return the first element.
1432
1433 Finally, we need to make sure that the type hierarchy is respected, which
1434 is the reason why a type formed by $\myarr$ will live in the least upper
1435 bound of the levels of argument and return type.
1436
1437 \subsubsection{$\myprod$, or dependent product}
1438 \label{sec:disju}
1439
1440 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1441      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1442      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1443      \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1444      \DisplayProof
1445
1446      \myderivspp
1447
1448     \begin{tabular}{cc}
1449       \AxiomC{$\myjud{\mytmm}{\mytya}$}
1450       \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1451       \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1452       \noLine
1453       \UnaryInfC{\phantom{$--$}}
1454       \DisplayProof
1455       &
1456       \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1457       \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1458       \noLine
1459       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1460       \DisplayProof
1461     \end{tabular}
1462 }
1463
1464 If dependent functions are a generalisation of $\myarr$ in the STLC,
1465 dependent products are a generalisation of $\myprod$ in the STLC.  The
1466 improvement is that the second element's type can depend on the value of
1467 the first element.  The correspondence with logic is through the
1468 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1469 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1470 The first element will be a number, and the second evidence that the
1471 number is even.  This highlights the fact that we are working in a
1472 constructive logic: if we have an existence proof, we can always ask for
1473 a witness.  This means, for instance, that $\neg \forall \neg$ is not
1474 equivalent to $\exists$.  Additionally, we need to specify the type of
1475 the second element (ranging over the first element) explicitly when
1476 using $\mypair{\myarg}{\myarg}$.
1477
1478 Another perhaps more `dependent' application of products, paired with
1479 $\mybool$, is to offer choice between different types.  For example we
1480 can easily recover disjunctions:
1481 \[
1482 \begin{array}{l}
1483   \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1484   \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1485   \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} \\
1486   \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1487   \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}})}
1488 \end{array}
1489 \]
1490
1491 \subsubsection{$\mytyc{W}$, or well-order}
1492 \label{sec:well-order}
1493
1494 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1495   \begin{tabular}{cc}
1496      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1497      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1498      \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1499      \DisplayProof
1500
1501      &
1502
1503      \AxiomC{$\myjud{\mytmt}{\mytya}$}
1504      \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1505      \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1506      \DisplayProof
1507    \end{tabular}
1508
1509      \myderivspp
1510
1511      \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1512      \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1513      \noLine
1514      \BinaryInfC{$\myjud{\myse{p}}{
1515        \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}}}}
1516      }$}
1517      \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1518      \DisplayProof
1519 }
1520
1521 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1522 let us represent inductive data in a general way.  We can form `nodes'
1523 of the shape \[\mytmt \mynode{\myb{x}}{\mytyb} \myse{f} :
1524 \myw{\myb{x}}{\mytya}{\mytyb}\] where $\mytmt$ is of type $\mytya$ and
1525 is the data present in the node, and $\myse{f}$ specifies a `child' of
1526 the node for each member of $\mysub{\mytyb}{\myb{x}}{\mytmt}$.  The
1527 $\myfun{rec}\ \myfun{with}$ acts as an induction principle on
1528 $\mytyc{W}$, given a predicate and a function dealing with the inductive
1529 case---we will gain more intuition about inductive data in Section
1530 \ref{sec:user-type}.
1531
1532 For example, if we want to form natural numbers, we can take
1533 \[
1534   \begin{array}{@{}l}
1535     \mytyc{Tr} : \mybool \myarr \mytyp_0 \\
1536     \mytyc{Tr} \myappsp \myb{b} \mapsto \myfun{if}\, \myb{b}\, \myfun{then}\, \myunit\, \myfun{else}\, \myempty \\
1537     \ \\
1538     \mynat : \mytyp_0 \\
1539     \mynat \mapsto \myw{\myb{b}}{\mybool}{(\mytyc{Tr}\myappsp\myb{b})}
1540   \end{array}
1541 \]
1542 Each node will contain a boolean.  If $\mytrue$, the number is non-zero,
1543 and we will have one child representing its predecessor, given that
1544 $\mytyc{Tr}$ will return $\myunit$.  If $\myfalse$, the number is zero,
1545 and we will have no predecessors (children), given the $\myempty$:
1546 \[
1547   \begin{array}{@{}l}
1548     \mydc{zero} : \mynat \\
1549     \mydc{zero} \mapsto \myfalse \mynodee (\myabs{\myb{x}}{\myabsurd{\mynat} \myappsp \myb{x}}) \\
1550     \ \\
1551     \mydc{suc} : \mynat \myarr \mynat \\
1552     \mydc{suc}\myappsp \myb{x} \mapsto \mytrue \mynodee (\myabs{\myarg}{\myb{x}})
1553   \end{array}
1554 \]
1555 And with a bit of effort, we can recover addition:
1556 \[
1557   \begin{array}{@{}l}
1558     \myfun{plus} : \mynat \myarr \mynat \myarr \mynat \\
1559     \myfun{plus} \myappsp \myb{x} \myappsp \myb{y} \mapsto \\
1560     \myind{2} \myfun{rec}\, \myb{x} / \myb{b}.\mynat \, \\
1561     \myind{2} \myfun{with}\, \myabs{\myb{b}}{\\
1562       \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) \\
1563       \myind{2}\myind{2}\myfun{then}\,(\myabs{\myarg\, \myb{f}}{\mydc{suc}\myappsp (\myapp{\myb{f}}{\mytt})})\, \myfun{else}\, (\myabs{\myarg\, \myarg}{\myb{y}})}
1564   \end{array}
1565   \]
1566   Note how we explicitly have to type the branches to make them match
1567   with the definition of $\mynat$.  This gives a taste of the clumsiness
1568   of $\mytyc{W}$-types but not the whole story.  Well-orders are
1569   inadequate not only because they are verbose, but also because they
1570   face deeper problems due to the weakness of the notion of equality
1571   present in most type theories, which we will present in the next
1572   section \citep{dybjer1997representing}.  The `better' equality we will
1573   present in Section \ref{sec:ott} helps but does not fully resolve
1574   these issues.\footnote{See \url{http://www.e-pig.org/epilogue/?p=324},
1575     which concludes with `W-types are a powerful conceptual tool, but
1576     they’re no basis for an implementation of recursive data types in
1577     decidable type theories.'}  For this reasons \mytyc{W}-types have
1578   remained nothing more than a reasoning tool, and practical systems
1579   must implement more expressive ways to represent data.
1580
1581 \section{The struggle for equality}
1582 \label{sec:equality}
1583
1584 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1585   schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1586
1587 In the previous section we learnt how a type checker for ITT needs
1588 a notion of \emph{definitional equality}.  Beyond this meta-theoretic
1589 notion, in this section we will explore the ways of expressing equality
1590 \emph{inside} the theory, as a reasoning tool available to the user.
1591 This area is the main concern of this thesis, and in general a very
1592 active research topic, since we do not have a fully satisfactory
1593 solution, yet.  As in the previous section, everything presented is
1594 formalised in Agda in Appendix \ref{app:agda-itt}.
1595
1596 \subsection{Propositional equality}
1597
1598 \begin{mydef}[Propositional equality] The syntax, reduction, and typing
1599   rules for propositional equality and related constructs are defined
1600   as:
1601 \end{mydef}
1602 \mynegder
1603 \noindent
1604 \begin{minipage}{0.5\textwidth}
1605 \mydesc{syntax}{ }{
1606   $
1607   \begin{array}{r@{\ }c@{\ }l}
1608     \mytmsyn & ::= & \cdots \\
1609              &  |  & \mypeq \myappsp \mytmsyn \myappsp \mytmsyn \myappsp \mytmsyn \mysynsep
1610                      \myapp{\myrefl}{\mytmsyn} \\
1611              &  |  & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1612   \end{array}
1613   $
1614 }
1615 \end{minipage} 
1616 \begin{minipage}{0.5\textwidth}
1617 \mydesc{\phantom{y}reduction:}{\mytmsyn \myred \mytmsyn}{
1618     $
1619     \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1620     $
1621   \vspace{1.05cm}
1622 }
1623 \end{minipage}
1624 \mynegder
1625 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1626     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1627     \AxiomC{$\myjud{\mytmm}{\mytya}$}
1628     \AxiomC{$\myjud{\mytmn}{\mytya}$}
1629     \TrinaryInfC{$\myjud{\mypeq \myappsp \mytya \myappsp  \mytmm \myappsp \mytmn}{\mytyp_l}$}
1630     \DisplayProof
1631
1632     \myderivspp
1633
1634     \begin{tabular}{cc}
1635       \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1636       \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}$}
1637       \DisplayProof
1638       &
1639       \AxiomC{$
1640         \begin{array}{c}
1641           \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\mypeq \myappsp \mytya \myappsp \myb{x} \myappsp \myb{y}}{\mytyp_l}}} \\
1642           \myjud{\myse{q}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1643         \end{array}
1644         $}
1645       \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1646       \DisplayProof
1647     \end{tabular}
1648 }
1649 \ \\
1650
1651 To express equality between two terms inside ITT, the obvious way to do
1652 so is to have equality to be a type.  Here we present what has survived
1653 as the dominating form of equality in systems based on ITT up since
1654 \cite{Martin-Lof1984} up to the present day.
1655
1656 Our type former is $\mypeq$, which given a type relates equal terms of
1657 that type.  $\mypeq$ has one introduction rule, $\myrefl$, which
1658 introduces an equality relation between definitionally equal terms.
1659
1660 Finally, we have one eliminator for $\mypeq$ , $\myjeqq$ (also known as
1661 `\myfun{J} axiom' in the literature).
1662 $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1663 \begin{itemize}
1664 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1665   $\mytya$) and a proof of their equality;
1666 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1667   $\myse{n}$) are equal;
1668 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$
1669   twice, plus the trivial proof by reflexivity showing that $\myse{m}$
1670   is equal to itself.
1671 \end{itemize}
1672 Given these ingredients, $\myjeqq$ returns a member of $\myse{P}$
1673 applied to $\mytmm$, $\mytmn$, and $\myse{q}$.  In other words $\myjeqq$
1674 takes a witness that $\myse{P}$ works with \emph{definitionally equal}
1675 terms, and returns a witness of $\myse{P}$ working with
1676 \emph{propositionally equal} terms.  Given its reduction rules,
1677 invocations of $\myjeqq$ will vanish when the equality proofs will
1678 reduce to invocations to reflexivity, at which point the arguments must
1679 be definitionally equal, and thus the provided
1680 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1681 can be returned.  This means that $\myjeqq$ will not compute with
1682 hypothetical proofs, which makes sense given that they might be false.
1683
1684 While the $\myjeqq$ rule is slightly convoluted, we can derive many more
1685 `friendly' rules from it, for example a more obvious `substitution' rule, that
1686 replaces equal for equal in predicates:
1687 \[
1688 \begin{array}{l}
1689 \myfun{subst} : \myfora{\myb{A}}{\mytyp}{\myfora{\myb{P}}{\myb{A} \myarr \mytyp}{\myfora{\myb{x}\ \myb{y}}{\myb{A}}{\mypeq \myappsp \myb{A} \myappsp \myb{x} \myappsp \myb{y} \myarr \myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{\myb{y}}}}} \\
1690 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1691   \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1692 \end{array}
1693 \]
1694 Once we have $\myfun{subst}$, we can easily prove more familiar laws
1695 regarding equality, such as symmetry, transitivity, congruence laws,
1696 etc.\footnote{For definitions of these functions, refer to Appendix \ref{app:itt-code}.}
1697
1698 \subsection{Common extensions}
1699
1700 Our definitional and propositional equalities can be enhanced in various
1701 ways.  Obviously if we extend the definitional equality we are also
1702 automatically extend propositional equality, given how $\myrefl$ works.
1703
1704 \subsubsection{$\eta$-expansion}
1705 \label{sec:eta-expand}
1706
1707 A simple extension to our definitional equality is achieved by $\eta$-expansion.
1708 Given an abstract variable $\myb{f} : \mytya \myarr \mytyb$ the aim is
1709 to have that $\myb{f} \mydefeq
1710 \myabss{\myb{x}}{\mytya}{\myapp{\myb{f}}{\myb{x}}}$.  We can achieve
1711 this by `expanding' terms depending on their types, a process known as
1712 \emph{quotation}---a term borrowed from the practice of
1713 \emph{normalisation by evaluation}, where we embed terms in some host
1714 language with an existing notion of computation, and then reify them
1715 back into terms, which will `smooth out' differences like the one above
1716 \citep{Abel2007}.
1717
1718 The same concept applies to $\myprod$, where we expand each inhabitant
1719 reconstructing it by getting its projections, so that $\myb{x}
1720 \mydefeq \mypair{\myfst \myappsp \myb{x}}{\mysnd \myappsp \myb{x}}$.
1721 Similarly, all one inhabitants of $\myunit$ and all zero inhabitants of
1722 $\myempty$ can be considered equal. Quotation can be performed in a
1723 type-directed way, as we will witness in Section \ref{sec:kant-irr}.
1724
1725 \begin{mydef}[Congruence and $\eta$-laws]
1726   To justify quotation in our type system we add a congruence law for
1727   abstractions and a similar law for products, plus the fact that all
1728   elements of $\myunit$ or $\myempty$ are equal.
1729 \end{mydef}
1730 \mynegder
1731 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1732   \begin{tabular}{cc}
1733     \AxiomC{$\myjudd{\myctx; \myb{y} : \mytya}{\myapp{\myse{f}}{\myb{x}} \mydefeq \myapp{\myse{g}}{\myb{x}}}{\mysub{\mytyb}{\myb{x}}{\myb{y}}}$}
1734     \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1735     \DisplayProof
1736     &
1737     \AxiomC{$\myjud{\mypair{\myapp{\myfst}{\mytmm}}{\myapp{\mysnd}{\mytmm}} \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1738     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1739     \DisplayProof
1740   \end{tabular}
1741
1742   \myderivspp
1743
1744   \begin{tabular}{cc}
1745   \AxiomC{$\myjud{\mytmm}{\myunit}$}
1746   \AxiomC{$\myjud{\mytmn}{\myunit}$}
1747   \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1748   \DisplayProof
1749   &
1750   \AxiomC{$\myjud{\mytmm}{\myempty}$}
1751   \AxiomC{$\myjud{\mytmn}{\myempty}$}
1752   \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myempty}$}
1753   \DisplayProof
1754   \end{tabular}
1755 }
1756
1757 \subsubsection{Uniqueness of identity proofs}
1758
1759 Another common but controversial addition to propositional equality is
1760 the $\myfun{K}$ axiom, which essentially states that all equality proofs
1761 are by reflexivity.
1762
1763 \begin{mydef}[$\myfun{K}$ axiom]\end{mydef}
1764 \mydesc{typing:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1765   \AxiomC{$
1766     \begin{array}{@{}c}
1767       \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\mypeq \myappsp \mytya \myappsp \myb{x}\myappsp \myb{x} \myarr \mytyp}} \\\
1768       \myjud{\mytmt}{\mytya} \hspace{1cm}
1769       \myjud{\myse{p}}{\myse{P} \myappsp \mytmt \myappsp (\myrefl \myappsp \mytmt)} \hspace{1cm}
1770       \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1771     \end{array}
1772     $}
1773   \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{t} \myappsp \myse{p} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1774   \DisplayProof
1775 }
1776
1777 \cite{Hofmann1994} showed that $\myfun{K}$ is not derivable from
1778 $\myjeqq$, and \cite{McBride2004} showed that it is needed to implement
1779 `dependent pattern matching', as first proposed by \cite{Coquand1992}.\footnote{See Section \ref{sec:future-work} for more on dependent pattern matching.}
1780 Thus, $\myfun{K}$ is derivable in the systems that implement dependent
1781 pattern matching, such as Epigram and Agda; but for example not in Coq.
1782
1783 $\myfun{K}$ is controversial mainly because it is at odds with
1784 equalities that include computational behaviour, most notably
1785 Voevodsky's \emph{Univalent Foundations}, which feature a \emph{univalence}
1786 axiom that identifies isomorphisms between types with propositional
1787 equality.  For example we would have two isomorphisms, and thus two
1788 equalities, between $\mybool$ and $\mybool$, corresponding to the two
1789 permutations---one is the identity, and one swaps the elements.  Given
1790 this, $\myfun{K}$ and univalence are inconsistent, and thus a form of
1791 dependent pattern matching that does not imply $\myfun{K}$ is subject of
1792 research.\footnote{More information about univalence can be found at
1793   \url{http://www.math.ias.edu/~vladimir/Site3/Univalent_Foundations.html}.}
1794
1795 \subsection{Limitations}
1796
1797 Propositional equality as described is quite restricted when
1798 reasoning about equality beyond the term structure, which is what definitional
1799 equality gives us (extensions notwithstanding).
1800
1801 \begin{mydef}[Extensional equality]
1802 Given two functions $\myse{f}$ and $\myse{g}$ of type $\mytya \myarr \mytyb$, they are are said to be \emph{extensionally equal} if
1803 \[ (\myb{x} {:} \mytya) \myarr \mypeq \myappsp \mytyb \myappsp (\myse{f} \myappsp \myb{x}) \myappsp (\myse{g} \myappsp \myb{x}) \]
1804 \end{mydef}
1805
1806 The problem is best exemplified by \emph{function extensionality}.  In
1807 mathematics, we would expect to be able to treat functions that give
1808 equal output for equal input as equal.  When reasoning in a mechanised
1809 framework we ought to be able to do the same: in the end, without
1810 considering the operational behaviour, all functions equal extensionally
1811 are going to be replaceable with one another.
1812
1813 However this is not the case, or in other words with the tools we have there is no closed term of type
1814 \[
1815 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1816     \myb{A} \myarr \myb{B}}{
1817         (\myfora{\myb{x}}{\myb{A}}{\mypeq \myappsp \myb{B} \myappsp (\myapp{\myb{f}}{\myb{x}}) \myappsp (\myapp{\myb{g}}{\myb{x}})}) \myarr
1818         \mypeq \myappsp (\myb{A} \myarr \myb{B}) \myappsp \myb{f} \myappsp \myb{g}
1819     }
1820 }
1821 \]
1822 To see why this is the case, consider the functions
1823 \[\myabs{\myb{x}}{0 \mathrel{\myfun{$+$}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 0}\]
1824 where $\myfun{$+$}$ is defined by recursion on the first argument,
1825 gradually destructing it to build up successors of the second argument.
1826 The two functions are clearly extensionally equal, and we can in fact
1827 prove that
1828 \[
1829 \myfora{\myb{x}}{\mynat}{\mypeq \myappsp \mynat \myappsp (0 \mathrel{\myfun{$+$}} \myb{x}) \myappsp (\myb{x} \mathrel{\myfun{$+$}} 0)}
1830 \]
1831 By induction on $\mynat$ applied to $\myb{x}$.  However, the two
1832 functions are not definitionally equal, and thus we will not be able to get
1833 rid of the quantification.
1834
1835 For the reasons given above, theories that offer a propositional equality
1836 similar to what we presented are called \emph{intensional}, as opposed
1837 to \emph{extensional}.  Most systems widely used today (such as Agda,
1838 Coq, and Epigram) are of the former kind.
1839
1840 This is quite an annoyance that often makes reasoning awkward or
1841 impossible to execute.  For example, we might want to represent terms of
1842 some language in Agda and give their denotation by embedding them in
1843 Agda---if we had $\lambda$-terms, functions will be Agda functions,
1844 application will be Agda's function application, and so on.  Then we
1845 would like to perform optimisation passes on the terms, and verify that
1846 they are sound by proving that the denotation of the optimised version
1847 is equal to the denotation of the starting term.
1848
1849 But if the embedding uses functions---and it probably will---we are
1850 stuck with an equality that identifies as equal only syntactically equal
1851 functions!  Since the point of optimising is about preserving the
1852 denotational but changing the operational behaviour of terms, our
1853 equality falls short of our needs.  Moreover, the problem extends to
1854 other fields beyond functions, such as bisimulation between processes
1855 specified by coinduction, or in general proving equivalences based on
1856 the behaviour of a term.
1857
1858 \subsection{Equality reflection}
1859
1860 One way to `solve' this problem is by identifying propositional equality
1861 with definitional equality.
1862
1863 \begin{mydef}[Equality reflection]\end{mydef}
1864 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1865     \AxiomC{$\myjud{\myse{q}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}$}
1866     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1867     \DisplayProof
1868 }
1869
1870 The \emph{equality reflection} rule is a very different rule from the
1871 ones we saw up to now: it links a typing judgement internal to the type
1872 theory to a meta-theoretic judgement that the type checker uses to work
1873 with terms.  It is easy to see the dangerous consequences that this
1874 causes:
1875 \begin{itemize}
1876 \item The rule is not syntax directed, and the type checker is
1877   presumably expected to come up with equality proofs when needed.
1878 \item More worryingly, type checking becomes undecidable also because
1879   computing under false assumptions becomes unsafe, since we derive any
1880   equality proof and then use equality reflection and the conversion
1881   rule to have terms of any type.
1882 \end{itemize}
1883
1884 Given these facts theories employing equality reflection, like NuPRL
1885 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1886 to keep the systems manageable.
1887
1888 For all its faults, equality reflection does allow us to prove extensionality,
1889 using the extensions we gave above.  Assuming that $\myctx$ contains
1890 \[\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}}}\]
1891 We can then derive
1892 \begin{prooftree}
1893   \mysmall
1894   \AxiomC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myb{q}}{\mypeq \myappsp \myb{A} \myappsp (\myapp{\myb{f}}{\myb{x}}) \myappsp (\myapp{\myb{g}}{\myb{x}})}$}
1895   \RightLabel{equality reflection}
1896   \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1897   \RightLabel{congruence for $\lambda$s}
1898   \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1899   \RightLabel{$\eta$-law for $\lambda$}
1900   \UnaryInfC{$\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}$}
1901   \RightLabel{$\myrefl$}
1902   \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\mypeq \myappsp (\myb{A} \myarr \myb{B}) \myappsp \myb{f} \myappsp \myb{g}}$}
1903 \end{prooftree}
1904 For this reason, theories employing equality reflection are often
1905 grouped under the name of \emph{Extensional Type Theory} (ETT).  Now,
1906 the question is: do we need to give up well-behavedness of our theory to
1907 gain extensionality?
1908
1909 \section{The observational approach}
1910 \label{sec:ott}
1911
1912 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1913   Theory} (OTT), promises to keep the well behavedness of ITT while
1914 being able to gain many useful equality proofs,\footnote{It is suspected
1915   that OTT gains \emph{all} the equality proofs of ETT, but no proof
1916   exists yet.} including function extensionality.  The main idea is to
1917 give the user the possibility to \emph{coerce} (or transport) values
1918 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1919 structurally that $\mytya$ and $\mytyb$ are equal; and providing a
1920 value-level equality based on similar principles.  Here we give an
1921 exposition which follows closely the original paper.
1922
1923 \subsection{A simpler theory, a propositional fragment}
1924
1925 \begin{mydef}[OTT's simple theory, with propositions]\ \end{mydef}
1926 \mynegder
1927 \mydesc{syntax}{ }{
1928     $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1929     $
1930     \begin{array}{r@{\ }c@{\ }l}
1931       \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1932                        \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1933       \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1934       \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1935     \end{array}
1936     $
1937 }
1938
1939 \mynegder
1940
1941 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1942   $
1943   \begin{array}{l@{}l@{\ }c@{\ }l}
1944     \myITE{\mytrue  &}{\mytya}{\mytyb} & \myred & \mytya \\
1945     \myITE{\myfalse &}{\mytya}{\mytyb} & \myred & \mytyb
1946   \end{array}
1947   $
1948 }
1949
1950 \mynegder
1951
1952 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1953   \begin{tabular}{cc}
1954     \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1955     \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1956     \DisplayProof
1957     &
1958     \AxiomC{$\myjud{\mytmt}{\mybool}$}
1959     \AxiomC{$\myjud{\mytya}{\mytyp}$}
1960     \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1961     \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1962     \DisplayProof
1963   \end{tabular}
1964 }
1965
1966 \mynegder
1967
1968 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1969     \begin{tabular}{ccc}
1970       \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1971       \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1972       \noLine
1973       \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1974       \DisplayProof
1975       &
1976       \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1977       \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1978       \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1979       \noLine
1980       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1981       \DisplayProof
1982       &
1983       \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1984       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1985       \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1986       \noLine
1987       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1988       \DisplayProof
1989     \end{tabular}
1990 }
1991
1992 Our foundation will be a type theory like the one of Section
1993 \ref{sec:itt}, with only one level: $\mytyp_0$.  In this context we will
1994 drop the $0$ and call $\mytyp_0$ $\mytyp$.  Moreover, since the old
1995 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1996 types thanks to the hierarchy (which is gone), we need to reintroduce an
1997 ad-hoc conditional for types, where the reduction rule is the obvious
1998 one.
1999
2000 However, we have an addition: a universe of \emph{propositions},
2001 $\myprop$.\footnote{Note that we do not need syntax for the type of props, $\myprop$, since the user cannot abstract over them.  In fact, we do not not need syntax for $\mytyp$ either, for the same reason.}  $\myprop$ isolates a fragment of types at large, and
2002 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$.
2003 \begin{mydef}[Proposition decoding]\ \end{mydef}
2004 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2005     \begin{tabular}{cc}
2006     $
2007     \begin{array}{l@{\ }c@{\ }l}
2008       \myprdec{\mybot} & \myred & \myempty \\
2009       \myprdec{\mytop} & \myred & \myunit
2010     \end{array}
2011     $
2012     &
2013     $
2014     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2015       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2016       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2017              \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2018     \end{array}
2019     $
2020     \end{tabular}
2021   } \\
2022   Propositions are what we call the types of \emph{proofs}, or types
2023   whose inhabitants contain no `data', much like $\myunit$.  The goal
2024   when isolating \mytyc{Prop} is twofold: erasing all top-level
2025   propositions when compiling; and identifying all equivalent
2026   propositions as the same, as we will see later.
2027
2028   Why did we choose what we have in $\myprop$?  Given the above
2029   criteria, $\mytop$ obviously fits the bill, since it has one element.
2030   A pair of propositions $\myse{P} \myand \myse{Q}$ still won't get us
2031   data, since if they both have one element the only possible pair is
2032   the one formed by said elements. Finally, if $\myse{P}$ is a
2033   proposition and we have $\myprfora{\myb{x}}{\mytya}{\myse{P}}$, the
2034   decoding will be a constant function for propositional content.  The
2035   only threat is $\mybot$, by which we can fabricate anything we want:
2036   however if we are consistent there will be no closed term of type
2037   $\mybot$ at, which is enough regarding proof erasure and
2038   term equality.
2039
2040   As an example of types that are \emph{not} propositional, consider
2041   $\mydc{Bool}$eans, which are the quintessential `relevant' data, since
2042   they are often used to decide the execution path of a program through
2043   $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}\myarg$ constructs.
2044
2045 \subsection{Equality proofs}
2046
2047 \begin{mydef}[Equality proofs and related operations]\ \end{mydef}
2048 \mynegder
2049 \mydesc{syntax}{ }{
2050     $
2051     \begin{array}{r@{\ }c@{\ }l}
2052       \mytmsyn & ::= & \cdots \mysynsep
2053       \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
2054       \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2055       \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
2056       \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
2057     \end{array}
2058     $
2059 }
2060
2061 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
2062   \begin{tabular}{cc}
2063     \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2064     \AxiomC{$\myjud{\mytmt}{\mytya}$}
2065     \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
2066     \DisplayProof
2067     &
2068   \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2069   \AxiomC{$\myjud{\mytmt}{\mytya}$}
2070   \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
2071   \DisplayProof
2072
2073   \end{tabular}
2074 }
2075
2076 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
2077     \begin{tabular}{cc}
2078       \AxiomC{$
2079         \begin{array}{l}
2080           \ \\
2081           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
2082         \end{array}
2083         $}
2084       \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
2085       \DisplayProof
2086       &
2087       \AxiomC{$
2088         \begin{array}{c}
2089           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
2090           \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
2091         \end{array}
2092         $}
2093     \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
2094     \DisplayProof
2095
2096     \end{tabular}
2097 }
2098
2099
2100 While isolating a propositional universe as presented can be a useful
2101 exercises on its own, what we are really after is a useful notion of
2102 equality.  In OTT we want to maintain that things judged to be equal are
2103 still always replaceable for one another with no additional
2104 changes. Note that this is not the same as saying that they are
2105 definitionally equal, since as we saw extensionally equal functions,
2106 while satisfying the above requirement, are not.
2107
2108 Towards this goal we introduce two equality constructs in
2109 $\myprop$---the fact that they are in $\myprop$ indicates that they
2110 indeed have no computational content.  The first construct, $\myarg
2111 \myeq \myarg$, relates types, the second,
2112 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values.  The
2113 value-level equality is different from our old propositional equality:
2114 instead of ranging over only one type, we might form equalities between
2115 values of different types---the usefulness of this construct will be
2116 clear soon.  In the literature this equality is known as `heterogeneous'
2117 or `John Major', since
2118
2119 \begin{quote}
2120   John Major's `classless society' widened people's aspirations to
2121   equality, but also the gap between rich and poor. After all, aspiring
2122   to be equal to others than oneself is the politics of envy. In much
2123   the same way, forms equations between members of any type, but they
2124   cannot be treated as equals (ie substituted) unless they are of the
2125   same type. Just as before, each thing is only equal to
2126   itself. \citep{McBride1999}.
2127 \end{quote}
2128
2129 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
2130 transport values between equal types; and $\myfun{coh}$ (`coherence')
2131 guarantees that $\myfun{coe}$ respects the value-level equality, or in
2132 other words that it really has no computational component: if we
2133 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
2134 will still be the same.
2135
2136 Before introducing the core ideas that make OTT work, let us distinguish
2137 between \emph{canonical} and \emph{neutral} terms and types.
2138
2139 \begin{mydef}[Canonical and neutral types and terms]
2140   In a type theory, \emph{neutral} terms are those formed by an
2141   abstracted variable or by an eliminator (including function
2142   application).  Everything else is \emph{canonical}.
2143
2144   In the current system, data constructors ($\mytt$, $\mytrue$,
2145   $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$, ...) will be
2146   canonical, the rest neutral.  Correspondingly, canonical types are
2147   those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
2148   and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$).
2149   Neutral types are those formed by
2150   $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
2151 \end{mydef}
2152 \begin{mydef}[Canonicity]
2153   If in a system all canonical types are inhabited by canonical terms
2154   the system is said to have the \emph{canonicity} property.
2155 \end{mydef}
2156 The current system, and well-behaved systems in general, has the
2157 canonicity property.  Another consequence of normalisation is that all
2158 closed terms will reduce to a canonical term.
2159
2160 \subsubsection{Type equality, and coercions}
2161
2162 The plan is to decompose type-level equalities between canonical types
2163 into decodable propositions containing equalities regarding the
2164 subterms.  So if are equating two product types, the equality will
2165 reduce to two subequalities regarding the first and second type.  Then,
2166 we can \myfun{coe}rce to transport values between equal types.
2167 Following the subequalities, \myfun{coe} will procede recursively on the
2168 subterms.
2169
2170 This interplay between the canonicity of equated types, type
2171 equalities, and \myfun{coe}, ensures that invocations of $\myfun{coe}$
2172 will vanish when we have evidence of the structural equality of the
2173 types we are transporting terms across.  If the type is neutral, the
2174 equality will not reduce and thus $\myfun{coe}$ will not reduce either.
2175 If we come across an equality between different canonical types, then we
2176 reduce the equality to bottom, making sure that no such proof can exist,
2177 and providing an `escape hatch' in $\myfun{coe}$.
2178
2179 \begin{figure}[t]
2180
2181 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
2182     $
2183       \begin{array}{c@{\ }c@{\ }c@{\ }l}
2184         \myempty & \myeq & \myempty & \myred \mytop \\
2185         \myunit  & \myeq &  \myunit & \myred  \mytop \\
2186         \mybool  & \myeq &  \mybool &   \myred  \mytop \\
2187         \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
2188         \multicolumn{4}{l}{
2189           \myind{2} \mytya_1 \myeq \mytya_2 \myand 
2190                   \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}]}
2191                   } \\
2192       \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
2193       \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
2194       \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
2195       \end{array}
2196     $
2197 }
2198 \myderivsp
2199 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
2200   $
2201   \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
2202     \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
2203     \mycoe & \myunit  & \myunit  & \myse{Q} & \myse{t} & \myred & \mytt \\
2204     \mycoe & \mybool  & \mybool  & \myse{Q} & \mytrue & \myred & \mytrue \\
2205     \mycoe & \mybool  & \mybool  & \myse{Q} & \myfalse & \myred & \myfalse \\
2206     \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2207              (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2208              \mytmt_1 & \myred & \\
2209              \multicolumn{7}{l}{
2210              \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
2211                \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
2212                            & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
2213                            & \myb{Q_A}      & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
2214                            & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
2215                            & \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}}} \\
2216                            & \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}} \\
2217                \mysyn{in}  & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
2218               \end{array}} \\
2219
2220     \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2221              (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2222              \mytmt & \myred &
2223            \cdots \\
2224
2225     \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2226              (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2227              \mytmt & \myred &
2228            \cdots \\
2229
2230     \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred & \myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
2231   \end{array}
2232   $
2233 }
2234 \caption{Reducing type equalities, and using them when
2235   $\myfun{coe}$rcing.}
2236 \label{fig:eqred}
2237 \end{figure}
2238
2239 \begin{mydef}[Type equalities reduction, and \myfun{coe}rcions] Figure
2240   \ref{fig:eqred} illustrates the rules to reduce equalities and to
2241   coerce terms.  We use a $\mysyn{let}$ syntax for legibility.
2242 \end{mydef}
2243 For ground types, the proof is the trivial element, and \myfun{coe} is
2244 the identity.  For $\myunit$, we can do better: we return its only
2245 member without matching on the term.  For the three type binders the
2246 choices we make in the type equality are dictated by the desire of
2247 writing the $\myfun{coe}$ in a natural way.
2248
2249 $\myprod$ is the easiest case: we decompose the proof into proofs that
2250 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
2251 proof that given equal values in the first element, the types of the
2252 second elements are equal too
2253 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
2254   \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]}$).\footnote{We
2255   are using $\myimpl$ to indicate a $\forall$ where we discard the
2256   quantified value.  We write $\mytyb_1[\myb{x_1}]$ to indicate that the
2257   $\myb{x_1}$ in $\mytyb_1$ is re-bound to the $\myb{x_1}$ quantified by
2258   the $\forall$, and similarly for $\myb{x_2}$ and $\mytyb_2$.}  This
2259 also explains the need for heterogeneous equality, since in the second
2260 proof we need to equate terms of possibly different types.  In the
2261 respective $\myfun{coe}$ case, since the types are canonical, we know at
2262 this point that the proof of equality is a pair of the shape described
2263 above.  Thus, we can immediately coerce the first element of the pair
2264 using the first element of the proof, and then instantiate the second
2265 element with the two first elements and a proof by coherence of their
2266 equality, since we know that the types are equal.
2267
2268 The cases for the other binders are omitted for brevity, but they follow
2269 the same principle with some twists to make $\myfun{coe}$ work with the
2270 generated proofs; the reader can refer to the paper for details.
2271
2272 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
2273 \label{sec:lazy}
2274
2275 It is important to notice that in the reduction rules for $\myfun{coe}$
2276 are never obstructed by the structure of the proofs.  With the exception
2277 of comparisons between different canonical types we never `pattern
2278 match' on the proof pairs, but always look at the projections.  This
2279 means that, as long as we are consistent, and thus as long as we don't
2280 have $\mybot$-inducing proofs, we can add propositional axioms for
2281 equality and $\myfun{coe}$ will still compute.  Thus, we can take
2282 $\myfun{coh}$ as axiomatic, and we can add back familiar useful equality
2283 rules:
2284
2285 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
2286   \AxiomC{$\myjud{\mytmt}{\mytya}$}
2287   \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mytmt}{\mytya}}}$}
2288   \DisplayProof
2289
2290   \myderivspp
2291
2292   \AxiomC{$\myjud{\mytya}{\mytyp}$}
2293   \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
2294   \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}}}}}$}
2295   \DisplayProof
2296 }
2297
2298 $\myrefl$ is the equivalent of the reflexivity rule in propositional
2299 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
2300 abstracting over a value we can substitute equal for equal---this lets
2301 us recover $\myfun{subst}$.  Note that while we need to provide ad-hoc
2302 rules in the restricted, non-hierarchical theory that we have, if our
2303 theory supports abstraction over $\mytyp$s we can easily add these
2304 axioms as top-level abstracted variables.
2305
2306 \subsubsection{Value-level equality}
2307
2308 \begin{mydef}[Value-level equality]\ \end{mydef}
2309 \mynegder
2310 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
2311   $
2312   \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
2313     (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
2314     (&\mytmt_1 & : & \myunit&) & \myeq & (&\mytmt_2 & : & \myunit&) & \myred \mytop \\
2315     (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
2316     (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
2317     (&\mytrue & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mybot \\
2318     (&\myfalse & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mybot \\
2319     (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
2320      & \multicolumn{11}{@{}l}{
2321       \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
2322       \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}}}
2323     } \\
2324    (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
2325      & \multicolumn{11}{@{}l}{
2326        \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
2327            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
2328            \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}]}
2329          }}
2330     } \\
2331    (&\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 \\
2332     (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
2333   \end{array}
2334   $
2335 }
2336
2337 As with type-level equality, we want value-level equality to reduce
2338 based on the structure of the compared terms.  When matching
2339 propositional data, such as $\myempty$ and $\myunit$, we automatically
2340 return the trivial type, since if a type has zero one members, all
2341 members will be equal.  When matching on data-bearing types, such as
2342 $\mybool$, we check that such data matches, and return bottom otherwise.
2343 When matching on records and functions, we rebuild the records to
2344 achieve $\eta$-expansion, and relate functions if they are extensionally
2345 equal---exactly what we wanted.  The case for \mytyc{W} is omitted but
2346 unsurprising, it checks that equal data in the nodes will bring equal
2347 children.
2348
2349 \subsection{Proof irrelevance and stuck coercions}
2350 \label{sec:ott-quot}
2351
2352 The last effort is required to make sure that proofs (members of
2353 $\myprop$) are \emph{irrelevant}.  Since they are devoid of
2354 computational content, we would like to identify all equivalent
2355 propositions as the same, in a similar way as we identified all
2356 $\myempty$ and all $\myunit$ as the same in section
2357 \ref{sec:eta-expand}.
2358
2359 Thus we will have a quotation that will not only perform
2360 $\eta$-expansion, but will also identify and mark proofs that could not
2361 be decoded (that is, equalities on neutral types).  Then, when
2362 comparing terms, marked proofs will be considered equal without
2363 analysing their contents, thus gaining irrelevance.
2364
2365 Moreover we can safely advance `stuck' $\myfun{coe}$rcions between
2366 non-canonical but definitionally equal types.  Consider for example
2367 \[
2368 \mycoee{(\myITE{\myb{b}}{\mynat}{\mybool})}{(\myITE{\myb{b}}{\mynat}{\mybool})}{\myb{x}}
2369 \]
2370 Where $\myb{b}$ and $\myb{x}$ are abstracted variables.  This
2371 $\myfun{coe}$ will not advance, since the types are not canonical.
2372 However they are definitionally equal, and thus we can safely remove the
2373 coerce and return $\myb{x}$ as it is.
2374
2375 \section{\mykant: the theory}
2376 \label{sec:kant-theory}
2377
2378 \epigraph{\emph{The construction itself is an art, its application to the world an evil parasite.}}{Luitzen Egbertus Jan `Bertus' Brouwer}
2379
2380 \mykant\ is an interactive theorem prover developed as part of this thesis.
2381 The plan is to present a core language which would be capable of serving as
2382 the basis for a more featureful system, while still presenting interesting
2383 features and more importantly observational equality.
2384
2385 We will first present the features of the system, along with motivations
2386 and trade-offs for the design decisions made. Then we describe the
2387 implementation we have developed in Section \ref{sec:kant-practice}.
2388 For an overview of the features of \mykant, see
2389 Section \ref{sec:contributions}, here we present them one by one.  The
2390 exception is type holes, which we do not describe holes rigorously, but
2391 provide more information about them in Section \ref{sec:type-holes}.
2392
2393 Note that in this section we will present \mykant\ terms in a fancy
2394 \LaTeX\ dress too keep up with the presentation, but every term, with its
2395 syntax reduced to the concrete syntax, is a valid \mykant\ term accepted
2396 by \mykant\ the software, and not only \mykant\ the theory.  Appendix
2397 \ref{app:kant-examples} displays most of the terms in this section in
2398 their concrete syntax.
2399
2400 \subsection{Bidirectional type checking}
2401
2402 We start by describing bidirectional type checking since it calls for
2403 fairly different typing rules that what we have seen up to now.  The
2404 idea is to have two kinds of terms: terms for which a type can always be
2405 inferred, and terms that need to be checked against a type.  A nice
2406 observation is that this duality is in correspondence with the notion of
2407 canonical and neutral terms: neutral terms
2408 (abstracted or defined variables, function application, record
2409 projections, primitive recursors, etc.) \emph{infer} types, canonical
2410 terms (abstractions, record/data types data constructors, etc.) need to
2411 be \emph{checked}.
2412
2413 To introduce the concept and notation, we will revisit the STLC in a
2414 bidirectional style.  The presentation follows \cite{Loh2010}.  The
2415 syntax for our bidirectional STLC is the same as the untyped
2416 $\lambda$-calculus, but with an extra construct to annotate terms
2417 explicitly---this will be necessary when dealing with top-level
2418 canonical terms.  The types are the same as those found in the normal
2419 STLC.
2420
2421 \begin{mydef}[Syntax for the annotated $\lambda$-calculus]\ \end{mydef}
2422 \mynegder
2423 \mydesc{syntax}{ }{
2424   $
2425   \begin{array}{r@{\ }c@{\ }l}
2426     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep (\mytmsyn : \mytysyn)
2427   \end{array}
2428   $
2429 }
2430
2431 We will have two kinds of typing judgements: \emph{inference} and
2432 \emph{checking}.  $\myinf{\mytmt}{\mytya}$ indicates that $\mytmt$
2433 infers the type $\mytya$, while $\mychk{\mytmt}{\mytya}$ can be checked
2434 against type $\mytya$.  The arrows signify the direction of the type
2435 checking---inference pushes types up, checking propagates types
2436 down.
2437
2438 The type of variables in context is inferred, and so are annotate terms.
2439 The type of applications is inferred too, propagating types down the
2440 applied term.  Abstractions are checked.  Finally, we have a rule to
2441 check the type of an inferrable term.
2442
2443 \begin{mydef}[Bidirectional type checking for the STLC]\ \end{mydef}
2444 \mynegder
2445 \mydesc{typing:}{\myctx \vdash \mytmsyn \Updownarrow \mytmsyn}{
2446   \begin{tabular}{cc}
2447     \AxiomC{$\myctx(x) = A$}
2448     \UnaryInfC{$\myinf{\myb{x}}{A}$}
2449     \DisplayProof
2450     &
2451     \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
2452     \UnaryInfC{$\mychk{\myabs{x}{\mytmt}}{(\myb{x} {:} \mytya) \myarr \mytyb}$}
2453     \DisplayProof
2454   \end{tabular}
2455
2456   \myderivspp
2457
2458   \begin{tabular}{ccc}
2459     \AxiomC{$\myinf{\mytmm}{\mytya \myarr \mytyb}$}
2460     \AxiomC{$\mychk{\mytmn}{\mytya}$}
2461     \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
2462     \DisplayProof
2463     &
2464     \AxiomC{$\mychk{\mytmt}{\mytya}$}
2465     \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2466     \DisplayProof
2467     &
2468     \AxiomC{$\myinf{\mytmt}{\mytya}$}
2469     \UnaryInfC{$\mychk{\mytmt}{\mytya}$}
2470     \DisplayProof
2471   \end{tabular}
2472 }
2473
2474 For example, if we wanted to type function composition (in this case for
2475 naturals), we would have to annotate the term:
2476 \[
2477 \begin{array}{@{}l}
2478   \myfun{comp} :  (\mynat \myarr \mynat) \myarr (\mynat \myarr \mynat) \myarr \mynat \myarr \mynat \\
2479   \myfun{comp} \mapsto (\myabs{\myb{f}\, \myb{g}\, \myb{x}}{\myb{f}\myappsp(\myb{g}\myappsp\myb{x})})
2480 \end{array}
2481 \]
2482 But we would not have to annotate functions passed to it, since the type would be propagated to the arguments:
2483 \[
2484    \myfun{comp}\myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 3}) \myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$*$}} 4}) \myappsp 42 : \mynat
2485 \]
2486
2487 \subsection{Base terms and types}
2488
2489 Let us begin by describing the primitives available without the user
2490 defining any data types, and without equality.  The way we handle
2491 variables and substitution is left unspecified, and explained in section
2492 \ref{sec:term-repr}, along with other implementation issues.  We are
2493 also going to give an account of the implicit type hierarchy separately
2494 in Section \ref{sec:term-hierarchy}, so as not to clutter derivation
2495 rules too much, and just treat types as impredicative for the time
2496 being.
2497
2498 \begin{mydef}[Syntax for base types in \mykant]\ \end{mydef}
2499 \mynegder
2500 \mydesc{syntax}{ }{
2501   $
2502   \begin{array}{r@{\ }c@{\ }l}
2503     \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
2504     &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
2505     \myabs{\myb{x}}{\mytmsyn} \mysynsep
2506     (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
2507     (\myann{\mytmsyn}{\mytmsyn}) \\
2508     \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
2509   \end{array}
2510   $
2511 }
2512
2513 The syntax for our calculus includes just two basic constructs:
2514 abstractions and $\mytyp$s.  Everything else will be user-defined.
2515 Since we let the user define values too, we will need a context capable
2516 of carrying the body of variables along with their type.
2517
2518 \begin{mydef}[Context validity]
2519 Bound names and defined names are treated separately in the syntax, and
2520 while both can be associated to a type in the context, only defined
2521 names can be associated with a body.
2522 \end{mydef}
2523 \mynegder
2524 \mydesc{context validity:}{\myvalid{\myctx}}{
2525     \begin{tabular}{ccc}
2526       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
2527       \UnaryInfC{$\myvalid{\myemptyctx}$}
2528       \DisplayProof
2529       &
2530       \AxiomC{$\mychk{\mytya}{\mytyp}$}
2531       \AxiomC{$\mynamesyn \not\in \myctx$}
2532       \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
2533       \DisplayProof
2534       &
2535       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2536       \AxiomC{$\myfun{f} \not\in \myctx$}
2537       \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
2538       \DisplayProof
2539     \end{tabular}
2540 }
2541
2542 Now we can present the reduction rules, which are unsurprising.  We have
2543 the usual function application ($\beta$-reduction), but also a rule to
2544 replace names with their bodies ($\delta$-reduction), and one to discard
2545 type annotations.  For this reason reduction is done in-context, as
2546 opposed to what we have seen in the past.
2547
2548 \begin{mydef}[Reduction rules for base types in \mykant]\ \end{mydef}
2549 \mynegder
2550 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
2551     \begin{tabular}{ccc}
2552       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2553       \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
2554                   \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
2555       \DisplayProof
2556       &
2557       \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
2558       \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
2559       \DisplayProof
2560       &
2561       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2562       \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
2563       \DisplayProof
2564     \end{tabular}
2565 }
2566
2567 We can now give types to our terms.  Although we include the usual
2568 conversion rule, we defer a detailed account of definitional equality to
2569 Section \ref{sec:kant-irr}.
2570
2571 \begin{mydef}[Bidirectional type checking for base types in \mykant]\ \end{mydef}
2572 \mynegder
2573 \mydesc{typing:}{\myctx \vdash \mytmsyn \Updownarrow \mytmsyn}{   
2574     \begin{tabular}{cccc}
2575       \AxiomC{$\myse{name} : A \in \myctx$}
2576       \UnaryInfC{$\myinf{\myse{name}}{A}$}
2577       \DisplayProof
2578       &
2579       \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
2580       \UnaryInfC{$\myinf{\myfun{f}}{A}$}
2581       \DisplayProof
2582       &
2583       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2584       \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2585       \DisplayProof
2586       &
2587       \AxiomC{$\myinf{\mytmt}{\mytya}$}
2588       \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
2589       \BinaryInfC{$\mychk{\mytmt}{\mytyb}$}
2590       \DisplayProof
2591     \end{tabular}
2592
2593     \myderivspp
2594
2595     \begin{tabular}{cc}
2596
2597       \AxiomC{\phantom{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}}
2598       \UnaryInfC{$\myinf{\mytyp}{\mytyp}$}
2599       \DisplayProof
2600       &
2601     \AxiomC{$\mychk{\mytya}{\mytyp}$}
2602     \AxiomC{$\mychkk{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
2603     \BinaryInfC{$\myinf{(\myb{x} {:} \mytya) \myarr \mytyb}{\mytyp}$}
2604     \DisplayProof
2605
2606     \end{tabular}
2607
2608
2609     \myderivspp
2610
2611     \begin{tabular}{cc}
2612       \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
2613       \AxiomC{$\mychk{\mytmn}{\mytya}$}
2614       \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
2615       \DisplayProof
2616
2617       &
2618
2619       \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
2620       \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2621       \DisplayProof
2622     \end{tabular}
2623
2624 }
2625
2626 \subsection{Elaboration}
2627
2628 As we mentioned, $\mykant$\ allows the user to define not only values
2629 but also custom data types and records.  \emph{Elaboration} consists of
2630 turning these declarations into workable syntax, types, and reduction
2631 rules.  The treatment of custom types in $\mykant$\ is heavily inspired
2632 by McBride's and McKinna's early work on Epigram \citep{McBride2004},
2633 although with some differences.
2634
2635 \subsubsection{Term vectors, telescopes, and assorted notation}
2636
2637 \begin{mydef}[Term vector]
2638   A \emph{term vector} is a series of terms.  The empty vector is
2639   represented by $\myemptyctx$, and a new element is added with
2640   $\myarg;\myarg$, similarly to contexts---$\vec{t};\mytmm$.
2641 \end{mydef}
2642
2643 We denote term vectors with the usual arrow notation,
2644 e.g. $vec{\mytmt}$, $\myvec{\mytmt};\mytmm$, etc.  We often use term
2645 vectors to refer to a series of term applied to another. For example
2646 $\mytyc{D} \myappsp \vec{A}$ is a shorthand for $\mytyc{D} \myappsp
2647 \mytya_1 \cdots \mytya_n$, for some $n$.  $n$ is consistently used to
2648 refer to the length of such vectors, and $i$ to refer to an index such
2649 that $1 \le i \le n$.
2650
2651 \begin{mydef}[Telescope]
2652   A \emph{telescope} is a series of typed bindings.  The empty telescope
2653   is represented by $\myemptyctx$, and a binding is added via
2654   $\myarg;\myarg$.
2655 \end{mydef}
2656
2657 To present the elaboration and operations on user defined data types, we
2658 frequently make use what \cite{Bruijn91} called \emph{telescopes}, a
2659 construct that will prove useful when dealing with the types of type and
2660 data constructors.  We refer to telescopes with $\mytele$, $\mytele'$,
2661 $\mytele_i$, etc.  If $\mytele$ refers to a telescope, $\mytelee$ refers
2662 to the term vector made up of all the variables bound by $\mytele$.
2663 $\mytele \myarr \mytya$ refers to the type made by turning the telescope
2664 into a series of $\myarr$.  For example we have that
2665 \[
2666    (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2667    (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2668 \]
2669
2670 We make use of various operations to manipulate telescopes:
2671 \begin{itemize}
2672 \item $\myhead(\mytele)$ refers to the first type appearing in
2673   $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2674   \myapp{\myfun{even}}{\myb{x}})) = \mynat$.  Similarly,
2675   $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2676   (1-indexed).
2677 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2678   first $i$ elements of $\mytele$:  $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2679   \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$.
2680 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2681   terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2682   \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2683   \myapp{\myfun{even}}{42})$.
2684 \end{itemize}
2685
2686 Additionally, when presenting syntax elaboration, We use $\mytmsyn^n$ to
2687 indicate a term vector composed of $n$ elements.  When clear from the
2688 context, we use term vectors to signify their length,
2689 e.g. $\mytmsyn^{\mytele}$, or $1 \le i \le \mytele$.
2690
2691 \subsubsection{Declarations syntax}
2692
2693 \begin{mydef}[Syntax of declarations in \mykant]\ \end{mydef}
2694 \mynegder
2695 \mydesc{syntax}{ }{
2696   $
2697   \begin{array}{r@{\ }c@{\ }l}
2698       \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2699                  &  |  & \mypost{\myb{x}}{\mytmsyn} \\
2700                  &  |  & \myadt{\mytyc{D}}{\myappsp \mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2701                  &  |  & \myreco{\mytyc{D}}{\myappsp \mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2702
2703       \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2704       \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2705   \end{array}
2706   $
2707 }
2708 In \mykant\ we have four kind of declarations:
2709
2710 \begin{description}
2711 \item[Defined value] A variable, together with a type and a body.
2712 \item[Abstract variable] An abstract variable, with a type but no body.
2713 \item[Inductive data] A \emph{data type}, with a \emph{type constructor}
2714   (denoted in blue, capitalised, sans serif: $\mytyc{D}$) various
2715   \emph{data constructors} (denoted in red, lowercase, sans serif:
2716   $\mydc{c}$), quite similar to what we find in Haskell.  A primitive
2717   \emph{eliminator} (or \emph{destructor}, or \emph{recursor}; denoted
2718   by green, lowercase, roman: \myfun{elim}) will be used to compute with
2719   each data type.
2720 \item[Record] A \emph{record}, which like data types consists of a type
2721   constructor but only one data constructor.  The user can also define
2722   various \emph{fields}, with no recursive occurrences of the type.  The
2723   functions extracting the fields' values from an instance of a record
2724   are called \emph{projections} (denoted in the same way as destructors).
2725 \end{description}
2726
2727 Elaborating defined variables consists of type checking the body against
2728 the given type, and updating the context to contain the new binding.
2729 Elaborating abstract variables and abstract variables consists of type
2730 checking the type, and updating the context with a new typed variable.
2731
2732 \begin{mydef}[Elaboration of defined and abstract variables]\ \end{mydef}
2733 \mynegder
2734 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2735     \begin{tabular}{cc}
2736       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2737       \AxiomC{$\myfun{f} \not\in \myctx$}
2738       \BinaryInfC{
2739         $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \  \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2740       }
2741       \DisplayProof
2742       &
2743       \AxiomC{$\mychk{\mytya}{\mytyp}$}
2744       \AxiomC{$\myfun{f} \not\in \myctx$}
2745       \BinaryInfC{
2746         $
2747           \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2748           \ \ \myelabf\ \  \myctx; \myfun{f} : \mytya
2749         $
2750       }
2751       \DisplayProof
2752     \end{tabular}
2753 }
2754
2755 \subsubsection{User defined types}
2756 \label{sec:user-type}
2757
2758 Elaborating user defined types is the real effort.  First, we will
2759 explain what we can define, with some examples.
2760
2761 \begin{description}
2762 \item[Natural numbers] To define natural numbers, we create a data type
2763   with two constructors: one with zero arguments ($\mydc{zero}$) and one
2764   with one recursive argument ($\mydc{suc}$):
2765   \[
2766   \begin{array}{@{}l}
2767     \myadt{\mynat}{ }{ }{
2768       \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2769     }
2770   \end{array}
2771   \]
2772   This is very similar to what we would write in Haskell:
2773   \begin{Verbatim}
2774 data Nat = Zero | Suc Nat
2775   \end{Verbatim}
2776   Once the data type is defined, $\mykant$\ will generate syntactic
2777   constructs for the type and data constructors, so that we will have
2778   \begin{center}
2779     \mysmall
2780     \begin{tabular}{ccc}
2781       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2782       \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2783       \DisplayProof
2784     &
2785       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2786       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2787       \DisplayProof
2788     &
2789       \AxiomC{$\mychk{\mytmt}{\mynat}$}
2790       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2791       \DisplayProof
2792     \end{tabular}
2793   \end{center}
2794   While in Haskell (or indeed in Agda or Coq) data constructors are
2795   treated the same way as functions, in $\mykant$\ they are syntax, so
2796   for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will give a
2797   syntax error.  This is necessary so that we can easily infer the type
2798   of polymorphic data constructors, as we will see later.
2799
2800   Moreover, each data constructor is prefixed by the type constructor
2801   name, since we need to retrieve the type constructor of a data
2802   constructor when type checking.  This measure aids in the presentation
2803   of the theory but it is not needed in the implementation, where
2804   we can have a dictionary to look up the type constructor corresponding
2805   to each data constructor.  When using data constructors in examples I
2806   will omit the type constructor prefix for brevity, in this case
2807   writing $\mydc{zero}$ instead of $\mynat.\mydc{zero}$ and $\mydc{suc}$ instead of
2808   $\mynat.\mydc{suc}$.
2809
2810   Along with user defined constructors, $\mykant$\ automatically
2811   generates an \emph{eliminator}, or \emph{destructor}, to compute with
2812   natural numbers: If we have $\mytmt : \mynat$, we can destruct
2813   $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2814   \begin{prooftree}
2815     \mysmall
2816     \AxiomC{$\mychk{\mytmt}{\mynat}$}
2817     \UnaryInfC{$
2818       \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2819         \begin{array}{@{}l}
2820           \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}}
2821           \end{array}
2822         }$}
2823   \end{prooftree}
2824   $\mynat.\myfun{elim}$ corresponds to the induction principle for
2825   natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2826   know that predicate holds for the base case
2827   ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2828   ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2829     \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2830   holds for any number.  As with the data constructors, we require the
2831   eliminator to be applied to the `destructed' element.
2832
2833   While the induction principle is usually seen as a mean to prove
2834   properties about numbers, in the intuitionistic setting it is also a
2835   mean to compute.  In this specific case $\mynat.\myfun{elim}$
2836   returns the base case if the provided number is $\mydc{zero}$, and
2837   recursively applies the inductive step if the number is a
2838   $\mydc{suc}$cessor:
2839   \[
2840   \begin{array}{@{}l@{}l}
2841     \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2842     \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})
2843   \end{array}
2844   \]
2845   The Haskell equivalent would be
2846   \begin{Verbatim}
2847 elim :: Nat -> a -> (Nat -> a -> a) -> a
2848 elim Zero    pz ps = pz
2849 elim (Suc n) pz ps = ps n (elim n pz ps)
2850 \end{Verbatim}
2851 Which buys us the computational behaviour, but not the reasoning power,
2852 since we cannot express the notion of a predicate depending on
2853 $\mynat$---the type system is far too weak.
2854
2855 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2856   lists are too similar to natural numbers to be interesting.
2857   \[
2858   \begin{array}{@{}l}
2859     \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2860       \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2861     }
2862   \end{array}
2863   \]
2864   Now the purpose of `constructors as syntax' can be explained: what would
2865   the type of $\mydc{leaf}$ be?  If we were to treat it as a `normal'
2866   term, we would have to specify the type parameter of the tree each
2867   time the constructor is applied:
2868   \[
2869   \begin{array}{@{}l@{\ }l}
2870     \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2871     \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2872   \end{array}
2873   \]
2874   The problem with this approach is that creating terms is incredibly
2875   verbose and dull, since we would need to specify the type parameter of
2876   $\mytyc{Tree}$ each time.  For example if we wished to create a
2877   $\mytree \myappsp \mynat$ with two nodes and three leaves, we would
2878   write
2879   \[
2880   \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)
2881   \]
2882   The redundancy of $\mynat$s is quite irritating.  Instead, if we treat
2883   constructors as syntactic elements, we can `extract' the type of the
2884   parameter from the type that the term gets checked against, much like
2885   what we do to type abstractions:
2886   \begin{center}
2887     \mysmall
2888     \begin{tabular}{cc}
2889       \AxiomC{$\mychk{\mytya}{\mytyp}$}
2890       \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2891       \DisplayProof
2892       &
2893       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2894       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2895       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2896       \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2897       \DisplayProof
2898     \end{tabular}
2899   \end{center}
2900   Which enables us to write, much more concisely
2901   \[
2902   \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}
2903   \]
2904   We gain an annotation, but we lose the myriad of types applied to the
2905   constructors.  Conversely, with the eliminator for $\mytree$, we can
2906   infer the type of the arguments given the type of the destructed:
2907   \begin{prooftree}
2908     \small
2909     \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2910     \UnaryInfC{$
2911       \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2912         \begin{array}{@{}l}
2913           (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2914           \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2915           ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2916           \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2917           \myapp{\myb{P}}{\mytmt}
2918         \end{array}
2919       }
2920       $}
2921   \end{prooftree}
2922   As expected, the eliminator embodies structural induction on trees.
2923   We have a base case for $\myb{P} \myappsp \mydc{leaf}$, and an
2924   inductive step that given two subtrees and the predicate applied to
2925   them needs to return the predicate applied to the tree formed by a
2926   node with the two subtrees as children.
2927
2928 \item[Empty type] We have presented types that have at least one
2929   constructors, but nothing prevents us from defining types with
2930   \emph{no} constructors:
2931   \[\myadt{\mytyc{Empty}}{ }{ }{ }\]
2932   What shall the `induction principle' on $\mytyc{Empty}$ be?  Does it
2933   even make sense to talk about induction on $\mytyc{Empty}$?
2934   $\mykant$\ does not care, and generates an eliminator with no `cases':
2935   \begin{prooftree}
2936     \mysmall
2937     \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2938     \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2939   \end{prooftree}
2940   which lets us write the $\myfun{absurd}$ that we know and love:
2941   \[
2942   \begin{array}{l@{}}
2943     \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \myempty \myarr \myb{A} \\
2944     \myfun{absurd}\myappsp \myb{A} \myappsp \myb{x} \mapsto \myempty.\myfun{elim} \myappsp \myb{x} \myappsp (\myabs{\myarg}{\myb{A}})
2945   \end{array}
2946   \]
2947
2948 \item[Ordered lists] Up to this point, the examples shown are nothing
2949   new to the \{Haskell, SML, OCaml, functional\} programmer.  However
2950   dependent types let us express much more than that.  A useful example
2951   is the type of ordered lists. There are many ways to define such a
2952   thing, but we will define ours to store the bounds of the list, making
2953   sure that $\mydc{cons}$ing respects that.
2954
2955   First, using $\myunit$ and $\myempty$, we define a type expressing the
2956   ordering on natural numbers, $\myfun{le}$---`less or equal'.
2957   $\myfun{le}\myappsp \mytmm \myappsp \mytmn$ will be inhabited only if
2958   $\mytmm \le \mytmn$:
2959   \[
2960     \begin{array}{@{}l}
2961       \myfun{le} : \mynat \myarr \mynat \myarr \mytyp \\
2962       \myfun{le} \myappsp \myb{n} \mapsto \\
2963           \myind{2} \mynat.\myfun{elim} \\
2964             \myind{2}\myind{2} \myb{n} \\
2965             \myind{2}\myind{2} (\myabs{\myarg}{\mynat \myarr \mytyp}) \\
2966             \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2967             \myind{2}\myind{2} (\myabs{\myb{n}\, \myb{f}\, \myb{m}}{
2968               \mynat.\myfun{elim} \myappsp \myb{m} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myb{m'}\, \myarg}{\myapp{\myb{f}}{\myb{m'}}})
2969                               })
2970     \end{array}
2971     \]
2972   We return $\myunit$ if the scrutinised is $\mydc{zero}$ (every
2973   number in less or equal than zero), $\myempty$ if the first number is
2974   a $\mydc{suc}$cessor and the second a $\mydc{zero}$, and we recurse if
2975   they are both successors.  Since we want the list to have possibly
2976   `open' bounds, for example for empty lists, we create a type for
2977   `lifted' naturals with a bottom ($\le$ everything but itself) and top
2978   ($\ge$ everything but itself) elements, along with an associated comparison
2979   function:
2980   \[
2981     \begin{array}{@{}l}
2982     \myadt{\mytyc{Lift}}{ }{ }{\mydc{bot} \mydcsep \mydc{lift} \myappsp \mynat \mydcsep \mydc{top}}\\
2983     \myfun{le'} : \mytyc{Lift} \myarr \mytyc{Lift} \myarr \mytyp\\
2984     \myfun{le'} \myappsp \myb{l_1} \mapsto \\
2985           \myind{2} \mytyc{Lift}.\myfun{elim} \\
2986             \myind{2}\myind{2} \myb{l_1} \\
2987             \myind{2}\myind{2} (\myabs{\myarg}{\mytyc{Lift} \myarr \mytyp}) \\
2988             \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2989             \myind{2}\myind{2} (\myabs{\myb{n_1}\, \myb{l_2}}{
2990               \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
2991             }) \\
2992             \myind{2}\myind{2} (\myabs{\myb{l_2}}{
2993               \mytyc{Lift}.\myfun{elim} \myappsp \myb{l_2} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myarg}{\myempty}) \myappsp \myunit
2994             })
2995     \end{array}
2996     \]
2997     Finally, we can define a type of ordered lists.  The type is
2998     parametrised over two \emph{values} representing the lower and upper
2999     bounds of the elements, as opposed to the \emph{type} parameters
3000     that we are used to in Haskell or similar languages.  An empty
3001     list will have to have evidence that the bounds are ordered, and
3002     each time we add an element we require the list to have a matching
3003     lower bound:
3004   \[
3005     \begin{array}{@{}l}
3006       \myadt{\mytyc{OList}}{\myappsp (\myb{low}\ \myb{upp} {:} \mytyc{Lift})}{\\ \myind{2}}{
3007           \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})
3008         }
3009     \end{array}
3010   \]
3011   Note that in the $\mydc{cons}$ constructor we quantify over the first
3012   argument, which will determine the type of the following
3013   arguments---again something we cannot do in systems like Haskell.  If
3014   we want we can then employ this structure to write and prove correct
3015   various sorting algorithms.\footnote{See this presentation by Conor
3016     McBride:
3017     \url{https://personal.cis.strath.ac.uk/conor.mcbride/Pivotal.pdf},
3018     and this blog post by the author:
3019     \url{http://mazzo.li/posts/AgdaSort.html}.}
3020
3021 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
3022   us another way to define types: $\mysyn{record}$.  A record is a
3023   data type with one constructor and `projections' to extract specific
3024   fields of the said constructor.
3025
3026   For example, we can recover dependent products:
3027   \[
3028   \begin{array}{@{}l}
3029     \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}}}
3030   \end{array}
3031   \]
3032   Here $\myfst$ and $\mysnd$ are the projections, with their respective
3033   types.  Note that each field can refer to the preceding fields---in
3034   this case we have the type of $\myfun{snd}$ depending on the value of
3035   $\myfun{fst}$.  A constructor will be automatically generated, under
3036   the name of $\mytyc{Prod}.\mydc{constr}$.  Dually to data types, we
3037   will omit the type constructor prefix for record projections.
3038
3039   Following the bidirectionality of the system, we have that projections
3040   (the destructors of the record) infer the type, while the constructor
3041   gets checked:
3042   \begin{center}
3043     \mysmall
3044     \begin{tabular}{cc}
3045       \AxiomC{$\mychk{\mytmm}{\mytya}$}
3046       \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
3047       \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
3048       \noLine
3049       \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
3050       \DisplayProof
3051       &
3052       \AxiomC{$\hspace{0.2cm}\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}\hspace{0.2cm}$}
3053       \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
3054       \noLine
3055       \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
3056       \DisplayProof
3057     \end{tabular}
3058   \end{center}
3059   What we have defined here is equivalent to ITT's dependent products.
3060
3061 \end{description}
3062
3063 \begin{figure}[p]
3064   \vspace{-.5cm}
3065     \mydesc{syntax}{ }{
3066       \small
3067       $
3068       \begin{array}{l}
3069         \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
3070       \end{array}
3071       $
3072     }
3073
3074     \mynegder
3075
3076   \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
3077     \small
3078       $
3079       \begin{array}{r@{\ }l}
3080          & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
3081         \myelabf &
3082         
3083         \begin{array}{r@{\ }c@{\ }l}
3084           \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
3085           \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
3086         \end{array}
3087       \end{array}
3088       $
3089   }
3090
3091     \mynegder
3092
3093   \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
3094         \small
3095
3096       \AxiomC{$
3097         \begin{array}{c}
3098           \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
3099           \mytyc{D} \not\in \myctx \\
3100           \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
3101           \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
3102         \end{array}
3103           $}
3104       \UnaryInfC{$
3105         \begin{array}{r@{\ }c@{\ }l}
3106           \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
3107           & & \vspace{-0.2cm} \\
3108           & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
3109           &          &
3110           \begin{array}{@{}r@{\ }l l}
3111             \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
3112             & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
3113             & \left.
3114               \begin{array}{@{}l}
3115                 \myind{3} \vdots \\
3116                 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
3117               \end{array} \right \}
3118             & \textbf{methods}  \\
3119             & \myapp{\myb{P}}{\myb{x}} &
3120           \end{array}
3121         \end{array}
3122         $}
3123       \DisplayProof \\ \vspace{0.2cm}\ \\
3124       $
3125         \begin{array}{@{}l l@{\ } l@{} r c l}
3126           \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
3127           & \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) \\
3128           & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
3129         \end{array}
3130         $
3131
3132   }
3133
3134     \mynegder
3135
3136   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{  
3137         \small
3138         $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
3139       \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3140       \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
3141       \BinaryInfC{$
3142           \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)}
3143         $}
3144       \DisplayProof \\ \vspace{0.2cm}\ \\
3145       $
3146         \begin{array}{@{}l l@{\ } l@{} r c l}
3147           \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
3148                          & \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) \\
3149                          & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
3150         \end{array}
3151         $
3152   }
3153
3154     \mynegder
3155
3156     \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
3157           \small
3158     $
3159     \begin{array}{r@{\ }c@{\ }l}
3160       \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
3161              & \myelabf &
3162
3163              \begin{array}{r@{\ }c@{\ }l}
3164                \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 \\
3165              \end{array}
3166     \end{array}
3167     $
3168 }
3169
3170     \mynegder
3171
3172 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
3173       \small
3174     \AxiomC{$
3175       \begin{array}{c}
3176         \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
3177         \mytyc{D} \not\in \myctx \\
3178         \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
3179       \end{array}
3180         $}
3181     \UnaryInfC{$
3182       \begin{array}{r@{\ }c@{\ }l}
3183         \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
3184         & & \vspace{-0.2cm} \\
3185         & \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}; \\
3186         & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
3187       \end{array}
3188       $}
3189     \DisplayProof
3190 }
3191
3192     \mynegder
3193
3194   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
3195         \small
3196           $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
3197           \AxiomC{$\mytyc{D} \in \myctx$}
3198           \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
3199           \DisplayProof
3200   }
3201
3202   \caption{Elaboration for data types and records.}
3203   \label{fig:elab}
3204 \end{figure}
3205
3206 \begin{mydef}[Elaboration for user defined types]
3207   Following the intuition given by the examples, the full elaboration
3208   machinery is presented Figure \ref{fig:elab}.
3209 \end{mydef}
3210 Our elaboration is essentially a modification of Figure 9 of
3211 \cite{McBride2004}. However, our data types are not inductive
3212 families,\footnote{See Section \ref{sec:future-work} for a brief
3213   description of inductive families.} we do bidirectional type checking
3214 by treating constructors/destructors as syntax, and we have records.
3215
3216 \begin{mydef}[Strict positivity]
3217   A inductive type declaration is \emph{strictly positive} if recursive
3218   occurrences of the type we are defining do not appear embedded
3219   anywhere in the domain part of any function in the types for the data
3220   constructors.
3221 \end{mydef}
3222 In data type declarations we allow recursive occurrences as long as they
3223 are strictly positive, which ensures the consistency of the theory.  To
3224 achieve that we employing a syntactic check to make sure that this is
3225 the case---in fact the check is stricter than necessary for simplicity,
3226 given that we allow recursive occurrences only at the top level of data
3227 constructor arguments.  For example a definition of the $\mytyc{W}$ type
3228 is accepted in Agda but rejected in \mykant.  This is to make the
3229 eliminator generation simpler, and in practice it is seldom an
3230 impediment.
3231
3232 Without these precautions, we can easily derive any type with no
3233 recursion:
3234 \begin{Verbatim}
3235 data Fix a = Fix (Fix a -> a) -- Negative occurrence of `Fix a'
3236 -- Term inhabiting any type `a'
3237 boom :: a
3238 boom = (\f -> f (Fix f)) (\x -> (\(Fix f) -> f) x x)
3239 \end{Verbatim}
3240 See \cite{Dybjer1991} for a more formal treatment of inductive
3241 definitions in ITT.
3242
3243 For what concerns records, recursive occurrences are disallowed.  The
3244 reason for this choice is answered by the reason for the choice of
3245 having records at all: we need records to give the user types with
3246 $\eta$-laws for equality, as we saw in Section \ref{sec:eta-expand}
3247 and in the treatment of OTT in Section \ref{sec:ott}.  If we tried to
3248 $\eta$-expand recursive data types, we would expand forever.
3249
3250 \begin{mydef}[Bidirectional type checking for elaborated types]
3251 To implement bidirectional type checking for constructors and
3252 destructors, we store their types in full in the context, and then
3253 instantiate when due.
3254 \end{mydef}
3255 \mynegder
3256 \mydesc{typing:}{\myctx
3257   \vdash \mytmsyn \Updownarrow \mytmsyn}{ \AxiomC{$
3258       \begin{array}{c}
3259         \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
3260         \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
3261         \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
3262         \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
3263         \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \ 
3264           (1 \le i \le \mytele'')
3265       \end{array}
3266       $}
3267     \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
3268     \DisplayProof
3269
3270     \myderivspp
3271
3272     \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3273     \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
3274       \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
3275     \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
3276     \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
3277         \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
3278         \myse{F})(\vec{A};\mytmt)}$}
3279     \DisplayProof
3280   }
3281 Note that for 0-ary type constructors, like $\mynat$, we do not need to
3282 check canonical terms: we can automatically infer that $\mydc{zero}$ and
3283 $\mydc{suc}\myappsp n$ are of type $\mynat$.  \mykant\ implements this measure, even
3284 if it is not shown in the typing rule for simplicity.
3285
3286 \subsubsection{Why user defined types?  Why eliminators?}
3287
3288 The hardest design choice in developing $\mykant$\ was to decide whether
3289 user defined types should be included, and how to handle them.  As we
3290 saw, while we can devise general structures like $\mytyc{W}$, they are
3291 unsuitable both for for direct usage and `mechanical' usage.  Thus most
3292 theorem provers in the wild provide some means for the user to define
3293 structures tailored to specific uses.
3294
3295 Even if we take user defined types for granted, while there is not much
3296 debate on how to handle records, there are two broad schools of thought
3297 regarding the handling of data types:
3298 \begin{description}
3299 \item[Fixed points and pattern matching] The road chosen by Agda and Coq.
3300   Functions are written like in Haskell---matching on the input and with
3301   explicit recursion.  An external check on the recursive arguments
3302   ensures that they are decreasing, and thus that all functions
3303   terminate.  This approach is the best in terms of user usability, but
3304   it is tricky to implement correctly.
3305
3306 \item[Elaboration into eliminators] The road chose by \mykant, and
3307   pioneered by the Epigram line of work.  The advantage is that we can
3308   reduce every data type to simple definitions which guarantee
3309   termination and are simple to reduce and type.  It is however more
3310   cumbersome to use than pattern matching, although \cite{McBride2004}
3311   has shown how to implement an expressive pattern matching interface on
3312   top of a larger set of combinators of those provided by \mykant.
3313
3314   We can go ever further down this road and elaborate the declarations
3315   for data types themselves to a small set of primitives, so that our `core'
3316   language will be very small and manageable
3317   \citep{dagand2012elaborating, chapman2010gentle}.
3318 \end{description}
3319
3320 We chose the safer and easier to implement path, given the time
3321 constraints and the higher confidence of correctness.  See also Section
3322 \ref{sec:future-work} for a brief overview of ways to extend or treat
3323 user defined types.
3324
3325 \subsection{Cumulative hierarchy and typical ambiguity}
3326 \label{sec:term-hierarchy}
3327
3328 Having a well founded type hierarchy is crucial if we want to retain
3329 consistency, otherwise we can break our type systems by proving bottom,
3330 as shown in Appendix \ref{app:hurkens}.
3331
3332 However, hierarchy as presented in section \ref{sec:itt} is a
3333 considerable burden on the user, on various levels.  Consider for
3334 example how we recovered disjunctions in Section \ref{sec:disju}: we
3335 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
3336 What if we wanted to form a disjunction containing something a
3337 $\mytyp_1$, or $\mytyp_{42}$?  Our definition would fail us, since
3338 $\mytyp_1 : \mytyp_2$.
3339
3340 \begin{figure}[b!]
3341
3342 \mydesc{cumulativity:}{\myctx \vdash \mytmsyn \mycumul \mytmsyn}{
3343   \begin{tabular}{ccc}
3344     \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
3345     \UnaryInfC{$\myctx \vdash \mytya \mycumul \mytyb$}
3346     \DisplayProof
3347     &
3348     \AxiomC{\phantom{$\myctx \vdash \mytya \mydefeq \mytyb$}}
3349     \UnaryInfC{$\myctx \vdash \mytyp_l \mycumul \mytyp_{l+1}$}
3350     \DisplayProof
3351     &
3352     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3353     \AxiomC{$\myctx \vdash \mytyb \mycumul \myse{C}$}
3354     \BinaryInfC{$\myctx \vdash \mytya \mycumul \myse{C}$}
3355     \DisplayProof
3356   \end{tabular}
3357
3358   \myderivspp
3359
3360   \begin{tabular}{ccc}
3361     \AxiomC{$\myjud{\mytmt}{\mytya}$}
3362     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3363     \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
3364     \DisplayProof
3365     &
3366     \AxiomC{$\myctx \vdash \mytya_1 \mydefeq \mytya_2$}
3367     \AxiomC{$\myctx; \myb{x} : \mytya_1 \vdash \mytyb_1 \mycumul \mytyb_2$}
3368     \BinaryInfC{$\myctx (\myb{x} {:} \mytya_1) \myarr \mytyb_1 \mycumul  (\myb{x} {:} \mytya_2) \myarr \mytyb_2$}
3369     \DisplayProof
3370   \end{tabular}
3371 }
3372 \caption{Cumulativity rules for base types in \mykant, plus a
3373   `conversion' rule for cumulative types.}
3374   \label{fig:cumulativity}
3375 \end{figure}
3376
3377 One way to solve this issue is a \emph{cumulative} hierarchy, where
3378 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$.  This way we retain
3379 consistency, while allowing for `large' definitions that work on small
3380 types too.
3381
3382 \begin{mydef}[Cumulativity for \mykant' base types]
3383   Figure \ref{fig:cumulativity} gives a formal definition of
3384   \emph{cumulativity} for the base types.  Similar measures can be taken
3385   for user defined types, withe the type living in the least upper bound
3386   of the levels where the types contained data live.
3387 \end{mydef}
3388 For example we might define our disjunction to be
3389 \[
3390   \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
3391 \]
3392 And hope that $\mytyp_{100}$ will be large enough to fit all the types
3393 that we want to use with our disjunction.  However, there are two
3394 problems with this.  First, clumsiness of having to manually specify the
3395 size of types is still there.  More importantly, if we want to use
3396 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
3397 make sure that those allow for types at least as large as
3398 $\mytyp_{100}$.
3399
3400 A better option is to employ a mechanised version of what Russell called
3401 \emph{typical ambiguity}: we let the user live under the illusion that
3402 $\mytyp : \mytyp$, but check that the statements about types are
3403 consistent under the hood.  $\mykant$\ implements this following the
3404 plan given by \cite{Huet1988}.  See also \cite{Harper1991} for a
3405 published reference, although describing a more complex system allowing
3406 for both explicit and explicit hierarchy at the same time.
3407
3408 We define a partial ordering on the levels, with both weak ($\le$) and
3409 strong ($<$) constraints, the laws governing them being the same as the
3410 ones governing $<$ and $\le$ for the natural numbers.  Each occurrence
3411 of $\mytyp$ is decorated with a unique reference.  We keep a set of
3412 constraints regarding the ordering of each occurrence of $\mytyp$, each
3413 represented by its unique reference.  We add new constraints as we type
3414 check, generating new references when needed.
3415
3416 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
3417 denotes the unique reference assigned to that term, we will generate a
3418 new fresh reference and return the type $\mytyp\, r_2$, adding the
3419 constraint $r_1 < r_2$ to the set.  When type checking $\myctx \vdash
3420 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
3421 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
3422 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
3423 set.
3424
3425 If at any point the constraint set becomes inconsistent, type checking
3426 fails.  Moreover, when comparing two $\mytyp$ terms---during the process
3427 of deciding definitional equality for two terms---we equate their
3428 respective references with two $\le$ constraints.  Implementation
3429 details are given in Section \ref{sec:hier-impl}.
3430
3431 Another more flexible but also more verbose alternative is the one
3432 chosen by Agda, where levels can be quantified so that the relationship
3433 between arguments and result in type formers can be explicitly
3434 expressed:
3435 \[
3436 \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}
3437 \]
3438 Inference algorithms to automatically derive this kind of relationship
3439 are currently subject of research.  We choose a less flexible but more
3440 concise way, since it is easier to implement and better understood.
3441
3442 \subsection{Observational equality, \mykant\ style}
3443
3444 There are two correlated differences between $\mykant$\ and the theory
3445 used to present OTT.  The first is that in $\mykant$ we have a type
3446 hierarchy, which lets us, for example, abstract over types.  The second
3447 is that we let the user define inductive types and records.
3448
3449 Reconciling propositions for OTT and a hierarchy had already been
3450 investigated by Conor McBride,\footnote{See
3451   \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.} and we
3452 follow some of his suggestions, with some innovation.  Most of the dirty
3453 work, as an extension of elaboration, is to handle reduction rules and
3454 coercions for data types---both type constructors and data constructors.
3455
3456 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
3457
3458 Before defining $\myprop$, we define some basic types inside $\mykant$,
3459 as the target for the $\myprop$ decoder.
3460 \begin{mydef}[\mykant' propositional prelude]\ \end{mydef}
3461 \[
3462 \begin{array}{@{}l}
3463   \myadt{\mytyc{Empty}}{}{ }{ } \\
3464   \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
3465   \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
3466   \ \\
3467
3468   \myreco{\mytyc{Unit}}{}{}{ } \\ \ \\
3469
3470   \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
3471 \end{array}
3472 \]
3473
3474 \begin{mydef}[Propositions and decoding]\ \end{mydef}
3475 \mynegder
3476 \mydesc{syntax}{ }{
3477   $
3478   \begin{array}{r@{\ }c@{\ }l}
3479     \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
3480     \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
3481   \end{array}
3482   $
3483 }
3484 \mynegder
3485 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
3486   \begin{tabular}{cc}
3487     $
3488     \begin{array}{l@{\ }c@{\ }l}
3489       \myprdec{\mybot} & \myred & \myempty \\
3490       \myprdec{\mytop} & \myred & \myunit
3491     \end{array}
3492     $
3493     &
3494     $
3495     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
3496       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \mytyc{Prod} \myappsp \myprdec{\myse{P}} \myappsp \myprdec{\myse{Q}} \\
3497       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
3498       \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
3499     \end{array}
3500     $
3501   \end{tabular}
3502 }
3503
3504 We will overload the $\myand$ symbol to define `nested' products, and
3505 $\myproj{n}$ to project elements from them, so that
3506 \[
3507 \begin{array}{@{}l}
3508 \mytya \myand \mytyb = \mytya \myand (\mytyb \myand \mytop) \\
3509 \mytya \myand \mytyb \myand \myse{C} = \mytya \myand (\mytyb \myand (\myse{C} \myand \mytop)) \\
3510 \myind{2} \vdots \\
3511 \myproj{1} : \myprdec{\mytya \myand \mytyb} \myarr \myprdec{\mytya} \\
3512 \myproj{2} : \myprdec{\mytya \myand \mytyb \myand \myse{C}} \myarr \myprdec{\mytyb} \\
3513 \myind{2} \vdots
3514 \end{array}
3515 \]
3516 And so on, so that $\myproj{n}$ will work with all products with at
3517 least than $n$ elements.  Logically a 0-ary $\myand$ will correspond to
3518 $\mytop$.
3519
3520 \subsubsection{Some OTT examples}
3521
3522 Before presenting the direction that $\mykant$\ takes, let us consider
3523 two examples of use-defined data types, and the result we would expect
3524 given what we already know about OTT, assuming the same propositional
3525 equalities.
3526
3527 \begin{description}
3528
3529 \item[Product types] Let us consider first the already mentioned
3530   dependent product, using the alternate name $\mysigma$\footnote{For
3531     extra confusion, `dependent products' are often called `dependent
3532     sums' in the literature, referring to the interpretation that
3533     identifies the first element as a `tag' deciding the type of the
3534     second element, which lets us recover sum types (disjuctions), as we
3535     saw in Section \ref{sec:depprod}.  Thus, $\mysigma$.} to
3536   avoid confusion with the $\mytyc{Prod}$ in the prelude:
3537   \[
3538   \begin{array}{@{}l}
3539     \myreco{\mysigma}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
3540   \end{array}
3541   \]
3542   First type-level equality.  The result we want is
3543   \[
3544     \begin{array}{@{}l}
3545       \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\
3546       \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}}}
3547     \end{array}
3548   \]
3549   The difference here is that in the original presentation of OTT the
3550   type binders are explicit, while here $\mytyb_1$ and $\mytyb_2$ are
3551   functions returning types.  We can do this thanks to the type
3552   hierarchy, and this hints at the fact that heterogeneous equality will
3553   have to allow $\mytyp$ `to the right of the colon'.  Indeed,
3554   heterogeneous equalities involving abstractions over types will
3555   provide the solution to simplify the equality above.
3556
3557   If we take, just like we saw previously in OTT
3558   \[
3559     \begin{array}{@{}l}
3560       \myjm{\myse{f}_1}{\myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}}{\myse{f}_2}{\myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}} \myred \\
3561       \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3562            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3563            \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}]}
3564          }}
3565     \end{array}
3566   \]
3567   Then we can simply have
3568   \[
3569     \begin{array}{@{}l}
3570       \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}
3571     \end{array}
3572   \]
3573   Which will reduce to precisely what we desire, but with an
3574   heterogeneous equalities relating types instead of values:
3575   \[
3576   \begin{array}{@{}l}
3577     \mytya_1 \myeq \mytya_2 \myand \myjm{\mytyb_1}{\mytya_1 \myarr \mytyp}{\mytyb_2}{\mytya_2 \myarr \mytyp} \myred \\
3578     \mytya_1 \myeq \mytya_2 \myand
3579     \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3580         \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3581         \myjm{\myapp{\mytyb_1}{\myb{x_1}}}{\mytyp}{\myapp{\mytyb_2}{\myb{x_2}}}{\mytyp}
3582       }}
3583   \end{array}
3584   \]
3585   If we pretend for the moment that those heterogeneous equalities were
3586   type equalities, things run smoothly. For what concerns coercions and
3587   quotation, things stay the same (apart from the fact that we apply to
3588   the second argument instead of substituting).  We can recognise
3589   records such as $\mysigma$ as such and employ projections in value
3590   equality and coercions; as to not impede progress if not necessary.
3591
3592 \item[Lists] Now for finite lists, which will give us a taste for data
3593   constructors:
3594   \[
3595   \begin{array}{@{}l}
3596     \myadt{\mylist}{\myappsp (\myb{A} {:} \mytyp)}{ }{\mydc{nil} \mydcsep \mydc{cons} \myappsp \myb{A} \myappsp (\myapp{\mylist}{\myb{A}})}
3597   \end{array}
3598   \]
3599   Type equality is simple---we only need to compare the parameter:
3600   \[
3601     \mylist \myappsp \mytya_1 \myeq \mylist \myappsp \mytya_2 \myred \mytya_1 \myeq \mytya_2
3602     \]
3603     For coercions, we transport based on the constructor, recycling the
3604     proof for the inductive occurrence:
3605   \[
3606     \begin{array}{@{}l@{\ }c@{\ }l}
3607       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mydc{nil} & \myred & \mydc{nil} \\
3608       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp (\mydc{cons} \myappsp \mytmm \myappsp \mytmn) & \myred & \\
3609       \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)}
3610     \end{array}
3611   \]
3612   Value equality is unsurprising---we match the constructors, and
3613   return bottom for mismatches.  However, we also need to equate the
3614   parameter in $\mydc{nil}$:
3615   \[
3616     \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
3617       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mytya_1 \myeq \mytya_2 \\
3618       (& \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 \\
3619       & \multicolumn{11}{@{}l}{ \myind{2}
3620         \myjm{\mytmm_1}{\mytya_1}{\mytmm_2}{\mytya_2} \myand \myjm{\mytmn_1}{\myapp{\mylist}{\mytya_1}}{\mytmn_2}{\myapp{\mylist}{\mytya_2}}
3621         } \\
3622       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot \\
3623       (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot
3624     \end{array}
3625   \]
3626 \end{description}
3627
3628 \subsubsection{Only one equality}
3629
3630 Given the examples above, a more `flexible' heterogeneous equality must
3631 emerge, since of the fact that in $\mykant$ we re-gain the possibility
3632 of abstracting and in general handling types in a way that was not
3633 possible in the original OTT presentation.  Moreover, we found that the
3634 rules for value equality work well if used with user defined type
3635 abstractions---for example in the case of dependent products we recover
3636 the original definition with explicit binders, in a natural manner.
3637
3638 \begin{mydef}[Propositions, coercions, coherence, equalities and
3639   equality reduction for \mykant] See Figure \ref{fig:kant-eq-red}.
3640 \end{mydef}
3641
3642 \begin{mydef}[Type equality in \mykant]
3643   We define $\mytya \myeq \mytyb$ as an abbreviation for
3644   $\myjm{\mytya}{\mytyp}{\mytyb}{\mytyp}$.
3645 \end{mydef}
3646
3647 In fact, we can drop a separate notion of type-equality, which will
3648 simply be served by $\myjm{\mytya}{\mytyp}{\mytyb}{\mytyp}$.  We shall
3649 still distinguish equalities relating types for hierarchical
3650 purposes. We exploit record to perform $\eta$-expansion.  Moreover,
3651 given the nested $\myand$s, values of data types with zero constructors
3652 (such as $\myempty$) and records with zero destructors (such as
3653 $\myunit$) will be automatically always identified as equal.  As in the
3654 original OTT, and for the same reasons, we can take $\myfun{coh}$ as
3655 axiomatic.
3656
3657
3658 \begin{figure}[p]
3659 \mydesc{syntax}{ }{
3660   \small
3661   $
3662   \begin{array}{r@{\ }c@{\ }l}
3663     \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
3664                      \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3665     \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3666   \end{array}
3667   $
3668 }
3669
3670 \mynegder
3671
3672 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
3673   \small
3674   \begin{tabular}{cc}
3675     \AxiomC{$\mychk{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3676     \AxiomC{$\mychk{\mytmt}{\mytya}$}
3677     \BinaryInfC{$\myinf{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
3678     \DisplayProof
3679     &
3680     \AxiomC{$\mychk{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3681     \AxiomC{$\mychk{\mytmt}{\mytya}$}
3682     \BinaryInfC{$\myinf{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
3683     \DisplayProof
3684   \end{tabular}
3685 }
3686
3687 \mynegder
3688
3689 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
3690   \small
3691     \begin{tabular}{cc}
3692       \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
3693       \UnaryInfC{$\myjud{\mytop}{\myprop}$}
3694       \noLine
3695       \UnaryInfC{$\myjud{\mybot}{\myprop}$}
3696       \DisplayProof
3697       &
3698       \AxiomC{$\myjud{\myse{P}}{\myprop}$}
3699       \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
3700       \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
3701       \noLine
3702       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
3703       \DisplayProof
3704     \end{tabular}
3705
3706     \myderivspp
3707
3708     \begin{tabular}{cc}
3709       \AxiomC{$
3710         \begin{array}{@{}c}
3711           \phantom{\myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}}} \\
3712           \myjud{\myse{A}}{\mytyp}\hspace{0.8cm}
3713           \myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}
3714         \end{array}
3715         $}
3716       \UnaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
3717       \DisplayProof
3718       &
3719       \AxiomC{$
3720         \begin{array}{c}
3721           \myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}} \\
3722           \myjud{\myse{B}}{\mytyp} \hspace{0.8cm} \myjud{\mytmn}{\myse{B}}
3723         \end{array}
3724         $}
3725       \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
3726       \DisplayProof
3727     \end{tabular}
3728 }
3729
3730 \mynegder
3731
3732 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
3733   \small
3734     \begin{tabular}{cc}
3735     \AxiomC{}
3736     \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
3737     \DisplayProof
3738     &
3739     \AxiomC{}
3740     \UnaryInfC{$\myctx \vdash \myjm{\myprdec{\myse{P}}}{\mytyp}{\myprdec{\myse{Q}}}{\mytyp} \myred \mytop$}
3741     \DisplayProof
3742     \end{tabular}
3743
3744   \myderivspp
3745
3746   \AxiomC{}
3747   \UnaryInfC{$
3748     \begin{array}{@{}r@{\ }l}
3749     \myctx \vdash &
3750     \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp}  \myred \\
3751     & \myind{2} \mytya_2 \myeq \mytya_1 \myand \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
3752         \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]
3753       }}
3754     \end{array}
3755     $}
3756   \DisplayProof
3757
3758   \myderivspp
3759
3760   \AxiomC{}
3761   \UnaryInfC{$
3762     \begin{array}{@{}r@{\ }l}
3763       \myctx \vdash &
3764       \myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}  \myred \\
3765       & \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3766           \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3767           \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}]}
3768         }}
3769     \end{array}
3770     $}
3771   \DisplayProof
3772   
3773
3774   \myderivspp
3775
3776   \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3777   \UnaryInfC{$
3778     \begin{array}{r@{\ }l}
3779       \myctx \vdash &
3780       \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp}  \myred \\
3781       & \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}))})
3782     \end{array}
3783     $}
3784   \DisplayProof
3785
3786   \myderivspp
3787
3788   \AxiomC{$
3789     \begin{array}{@{}c}
3790       \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
3791       \mytyc{D}.\mydc{c} : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \hspace{0.8cm}
3792       \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
3793       \mytele_B = (\mytele;\mytele')\vec{B}
3794     \end{array}
3795     $}
3796   \UnaryInfC{$
3797     \begin{array}{@{}l@{\ }l}
3798       \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 \\
3799       & \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}))})
3800     \end{array}
3801     $}
3802   \DisplayProof
3803
3804   \myderivspp
3805
3806   \AxiomC{$\mydataty(\mytyc{D}, \myctx)$}
3807   \UnaryInfC{$
3808       \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
3809     $}
3810   \DisplayProof
3811
3812   \myderivspp
3813
3814   \AxiomC{$
3815     \begin{array}{@{}c}
3816       \myisreco(\mytyc{D}, \myctx)\hspace{0.8cm}
3817       \mytyc{D}.\myfun{f}_i : \mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i  \in \myctx\\
3818     \end{array}
3819     $}
3820   \UnaryInfC{$
3821     \begin{array}{@{}l@{\ }l}
3822       \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})})
3823     \end{array}
3824     $}
3825   \DisplayProof
3826   
3827   \myderivspp
3828   \AxiomC{}
3829   \UnaryInfC{$\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb}  \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
3830   \DisplayProof
3831 }
3832 \caption{Propositions and equality reduction in $\mykant$.  We assume
3833   the presence of $\mydataty$ and $\myisreco$ as operations on the
3834   context to recognise whether a user defined type is a data type or a
3835   record.}
3836   \label{fig:kant-eq-red}
3837 \end{figure}
3838
3839 \subsubsection{Coercions}
3840
3841 For coercions the algorithm is messier and not reproduced here for lack
3842 of a decent notation---the details are hairy but uninteresting.  To give
3843 an idea of the possible complications, let us conceive a type that
3844 showcases trouble not arising in the previous examples.
3845 \[
3846 \begin{array}{@{}l}
3847 \myadt{\mytyc{Max}}{\myappsp (\myb{A} {:} \mynat \myarr \mytyp) \myappsp (\myb{B} {:} (\myb{x} {:} \mynat) \myarr \myb{A} \myappsp \myb{x} \myarr \mytyp) \myappsp (\myb{k} {:} \mynat)}{ \\ \myind{2}}{
3848   \mydc{max} \myappsp (\myb{A} \myappsp \myb{k}) \myappsp (\myb{x} {:} \mynat) \myappsp (\myb{a} {:} \myb{A} \myappsp \myb{x}) \myappsp (\myb{B} \myappsp \myb{x} \myappsp \myb{a})
3849 }
3850 \end{array}
3851 \]
3852 For type equalities we will have
3853 \[
3854 \begin{array}{@{}l@{\ }l}
3855   \myjm{\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1}{\mytyp}{\mytyc{Max} \myappsp \mytya_2 \myappsp \myappsp \mytyb_2 \myappsp \myse{k}_2}{\mytyp} & \myred \\[0.2cm]
3856   \begin{array}{@{}l}
3857     \myjm{\mytya_1}{\mynat \myarr \mytyp}{\mytya_2}{\mynat \myarr \mytyp} \myand \\
3858     \myjm{\mytyb_1}{(\myb{x} {:} \mynat) \myarr \mytya_1 \myappsp \myb{x} \myarr \mytyp}{\mytyb_2}{(\myb{x} {:} \mynat) \myarr \mytya_2 \myappsp \myb{x} \myarr \mytyp} \\
3859     \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3860   \end{array} & \myred \\[0.7cm]
3861   \begin{array}{@{}l}
3862     (\mynat \myeq \mynat \myand  (\myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \myapp{\mytya_1}{\myb{x_1}} \myeq \myapp{\mytya_2}{\myb{x_2}}})) \myand \\
3863     (\mynat \myeq \mynat \myand \left(
3864     \begin{array}{@{}l}
3865       \myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl  \\ \myjm{\mytyb_1 \myappsp \myb{x_1}}{\mytya_1 \myappsp \myb{x_1} \myarr \mytyp}{\mytyb_2 \myappsp \myb{x_2}}{\mytya_2 \myappsp \myb{x_2} \myarr \mytyp}}
3866     \end{array}
3867     \right)) \myand \\
3868     \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3869   \end{array} & \myred \\[0.9cm]
3870   \begin{array}{@{}l}
3871     (\mytop \myand  (\myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \myapp{\mytya_1}{\myb{x_1}} \myeq \myapp{\mytya_2}{\myb{x_2}}})) \myand \\
3872     (\mytop \myand \left(
3873     \begin{array}{@{}l}
3874       \myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl  \\
3875         \myprfora{\myb{y_1}}{\mytya_1 \myappsp \myb{x_1}}{\myprfora{\myb{y_2}}{\mytya_2 \myappsp \myb{x_2}}{\myjm{\myb{y_1}}{\mytya_1 \myappsp \myb{x_1}}{\myb{y_2}}{\mytya_2 \myappsp \myb{x_2}} \myimpl  \\
3876             \mytyb_1 \myappsp \myb{x_1} \myappsp \myb{y_1} \myeq \mytyb_2 \myappsp \myb{x_2} \myappsp \myb{y_2}}}}
3877     \end{array}
3878     \right)) \myand \\
3879     \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3880   \end{array} & 
3881 \end{array}
3882 \]
3883 The result, while looking complicated, is actually saying something
3884 simple---given equal inputs, the parameters for $\mytyc{Max}$ will
3885 return equal types.  Moreover, we have evidence that the two $\myb{k}$
3886 parameters are equal.  When coercing, we need to mechanically generate
3887 one proof of equality for each argument, and then coerce:
3888 \[
3889 \begin{array}{@{}l}
3890 \mycoee{(\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1)}{(\mytyc{Max} \myappsp \mytya_2 \myappsp \mytyb_2 \myappsp \myse{k}_2)}{\myse{Q}}{(\mydc{max} \myappsp \myse{ak}_1 \myappsp \myse{n}_1 \myappsp \myse{a}_1 \myappsp \myse{b}_1)} \myred \\
3891 \myind{2}
3892 \begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
3893   \mysyn{let} & \myb{Q_{Ak}} & \mapsto & \myhole{?} : \myprdec{\mytya_1 \myappsp \myse{k}_1 \myeq \mytya_2 \myappsp \myse{k}_2} \\
3894               & \myb{ak_2}    & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{k}_1)}{(\mytya_2 \myappsp \myse{k}_2)}{\myb{Q_{Ak}}}{\myse{ak_1}} : \mytya_1 \myappsp \myse{k}_2 \\
3895               & \myb{Q_{\mathbb{N}}} & \mapsto & \myhole{?} : \myprdec{\mynat \myeq \mynat} \\
3896               & \myb{n_2} & \mapsto & \mycoee{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}} : \mynat \\
3897               & \myb{Q_A} & \mapsto & \myhole{?} : \myprdec{\mytya_1 \myappsp \myse{n_1} \myeq \mytya_2 \myappsp \myb{n_2}} \\
3898               & \myb{a_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myb{n_2})}{\myb{Q_A}} :  \mytya_2 \myappsp \myb{n_2} \\
3899               & \myb{Q_B} & \mapsto & \myhole{?} : \myprdec{\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a}_1 \myeq \mytyb_1 \myappsp \myb{n_2} \myappsp \myb{a_2}} \\
3900               & \myb{b_2} & \mapsto & \mycoee{(\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a_1})}{(\mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2})}{\myb{Q_B}} :  \mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2} \\
3901   \mysyn{in} & \multicolumn{3}{@{}l}{\mydc{max} \myappsp \myb{ak_2} \myappsp \myb{n_2} \myappsp \myb{a_2} \myappsp \myb{b_2}}
3902 \end{array}
3903 \end{array}
3904 \]
3905 For equalities regarding types that are external to the data type we can
3906 derive a proof by reflexivity by invoking $\mydc{refl}$ as defined in
3907 Section \ref{sec:lazy}, and the instantiate arguments if we need too.
3908 In this case, for $\mynat$, we do not have any arguments.  For
3909 equalities concerning arguments of the type constructor or already
3910 coerced arguments of the type constructor we have to refer to the right
3911 proof and use $\mycoh$erence when due, which is where the technical
3912 annoyance lies:
3913 \[
3914 \begin{array}{@{}l}
3915 \mycoee{(\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1)}{(\mytyc{Max} \myappsp \mytya_2 \myappsp \mytyb_2 \myappsp \myse{k}_2)}{\myse{Q}}{(\mydc{max} \myappsp \myse{ak}_1 \myappsp \myse{n}_1 \myappsp \myse{a}_1 \myappsp \myse{b}_1)} \myred \\
3916 \myind{2}
3917 \begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
3918   \mysyn{let} & \myb{Q_{Ak}} & \mapsto & (\myproj{2} \myappsp (\myproj{1} \myappsp \myse{Q})) \myappsp \myse{k_1} \myappsp \myse{k_2} \myappsp (\myproj{3} \myappsp \myse{Q}) : \myprdec{\mytya_1 \myappsp \myse{k}_1 \myeq \mytya_2 \myappsp \myse{k}_2} \\
3919               & \myb{ak_2}    & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{k}_1)}{(\mytya_2 \myappsp \myse{k}_2)}{\myb{Q_{Ak}}}{\myse{ak_1}} : \mytya_1 \myappsp \myse{k}_2 \\
3920               & \myb{Q_{\mathbb{N}}} & \mapsto & \mydc{refl} \myappsp \mynat : \myprdec{\mynat \myeq \mynat} \\
3921               & \myb{n_2} & \mapsto & \mycoee{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}} : \mynat \\
3922               & \myb{Q_A} & \mapsto & (\myproj{2} \myappsp (\myproj{1} \myappsp \myse{Q})) \myappsp \myse{n_1} \myappsp \myb{n_2} \myappsp (\mycohh{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}}) : \myprdec{\mytya_1 \myappsp \myse{n_1} \myeq \mytya_2 \myappsp \myb{n_2}} \\
3923               & \myb{a_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myb{n_2})}{\myb{Q_A}} :  \mytya_2 \myappsp \myb{n_2} \\
3924               & \myb{Q_B} & \mapsto & (\myproj{2} \myappsp (\myproj{2} \myappsp \myse{Q})) \myappsp \myse{n_1} \myappsp \myb{n_2} \myappsp \myb{Q_{\mathbb{N}}} \myappsp \myse{a_1} \myappsp \myb{a_2} \myappsp (\mycohh{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myse{n_2})}{\myb{Q_A}}{\myse{a_1}}) : \myprdec{\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a}_1 \myeq \mytyb_1 \myappsp \myb{n_2} \myappsp \myb{a_2}} \\
3925               & \myb{b_2} & \mapsto & \mycoee{(\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a_1})}{(\mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2})}{\myb{Q_B}} :  \mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2} \\
3926   \mysyn{in} & \multicolumn{3}{@{}l}{\mydc{max} \myappsp \myb{ak_2} \myappsp \myb{n_2} \myappsp \myb{a_2} \myappsp \myb{b_2}}
3927 \end{array}
3928 \end{array}
3929 \]
3930
3931 \subsubsection{$\myprop$ and the hierarchy}
3932
3933 We shall have, at each universe level, not only a $\mytyp_l$ but also a
3934 $\myprop_l$.  Where will propositions placed in the type hierarchy?  The
3935 main indicator is the decoding operator, since it converts into things
3936 that already live in the hierarchy.  For example, if we have
3937 \[
3938   \myprdec{\mynat \myarr \mybool \myeq \mynat \myarr \mybool} \myred
3939   \mytop \myand ((\myb{x}\, \myb{y} : \mynat) \myarr \mytop \myarr \mytop)
3940 \]
3941 we will better make sure that the `to be decoded' is at level compatible
3942 (read: larger) with its reduction.  In the example above, we will have
3943 that proposition to be at least as large as the type of $\mynat$, since
3944 the reduced proof will abstract over it.  Pretending that we had
3945 explicit, non cumulative levels, it would be tempting to have
3946 \begin{center}
3947 \begin{tabular}{cc}
3948   \AxiomC{$\myjud{\myse{Q}}{\myprop_l}$}
3949   \UnaryInfC{$\myjud{\myprdec{\myse{Q}}}{\mytyp_l}$}
3950   \DisplayProof
3951 &
3952   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3953   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3954   \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3955   \DisplayProof
3956 \end{tabular}
3957 \end{center}
3958 $\mybot$ and $\mytop$ living at any level, $\myand$ and $\forall$
3959 following rules similar to the ones for $\myprod$ and $\myarr$ in
3960 Section \ref{sec:itt}. However, we need to be careful with value
3961 equality since for example we have that
3962 \[
3963   \myprdec{\myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}}
3964   \myred
3965   \myfora{\myb{x_1}}{\mytya_1}{\myfora{\myb{x_2}}{\mytya_2}{\cdots}}
3966 \]
3967 where the proposition decodes into something of at least type $\mytyp_l$, where
3968 $\mytya_l : \mytyp_l$ and $\mytyb_l : \mytyp_l$.  We can resolve this
3969 tension by making all equalities larger:
3970 \begin{prooftree}
3971   \AxiomC{$\myjud{\mytmm}{\mytya}$}
3972   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3973   \AxiomC{$\myjud{\mytmn}{\mytyb}$}
3974   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3975   \QuaternaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
3976 \end{prooftree}
3977 This is disappointing, since type equalities will be needlessly large:
3978 $\myprdec{\myjm{\mytya}{\mytyp_l}{\mytyb}{\mytyp_l}} : \mytyp_{l + 1}$.
3979
3980 However, considering that our theory is cumulative, we can do better.
3981 Assuming rules for $\myprop$ cumulativity similar to the ones for
3982 $\mytyp$, we will have (with the conversion rule reproduced as a
3983 reminder):
3984 \begin{center}
3985   \begin{tabular}{cc}
3986     \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3987     \AxiomC{$\myjud{\mytmt}{\mytya}$}
3988     \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
3989     \DisplayProof
3990     &
3991     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3992     \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3993     \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3994     \DisplayProof
3995   \end{tabular}
3996
3997   \myderivspp
3998
3999   \AxiomC{$\myjud{\mytmm}{\mytya}$}
4000   \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
4001   \AxiomC{$\myjud{\mytmn}{\mytyb}$}
4002   \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
4003   \AxiomC{$\mytya$ and $\mytyb$ are not $\mytyp_{l'}$}
4004   \QuinaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
4005   \DisplayProof
4006 \end{center}
4007
4008 That is, we are small when we can (type equalities) and large otherwise.
4009 This would not work in a non-cumulative theory because subject reduction
4010 would not hold.  Consider for instance
4011 \[
4012   \myjm{\mynat}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}{\mybool}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}
4013   : \myprop_1
4014 \]
4015 which reduces to
4016 \[\myjm{\mynat}{\mytyp_0}{\mybool}{\mytyp_0} : \myprop_0 \]
4017 We need members of $\myprop_0$ to be members of $\myprop_1$ too, which
4018 will be the case with cumulativity.  This buys us a cheap type level
4019 equality without having to replicate functionality with a dedicated
4020 construct.
4021
4022 \subsubsection{Quotation and definitional equality}
4023 \label{sec:kant-irr}
4024
4025 Now we can give an account of definitional equality, by explaining how
4026 to perform quotation (as defined in Section \ref{sec:eta-expand})
4027 towards the goal described in Section \ref{sec:ott-quot}.
4028
4029 We want to:
4030 \begin{itemize}
4031 \item Perform $\eta$-expansion on functions and records.
4032
4033 \item As a consequence of the previous point, identify all records with
4034 no projections as equal, since they will have only one element.
4035
4036 \item Identify all members of types with no constructors (and thus no
4037   elements) as equal.
4038
4039 \item Identify all equivalent proofs as equal---with `equivalent proof'
4040 we mean those proving the same propositions.
4041
4042 \item Advance coercions working across definitionally equal types.
4043 \end{itemize}
4044 Towards these goals and following the intuition between bidirectional
4045 type checking we define two mutually recursive functions, one quoting
4046 canonical terms against their types (since we need the type to type check
4047 canonical terms), one quoting neutral terms while recovering their
4048 types.
4049 \begin{mydef}[Quotation for \mykant]
4050 The full procedure for quotation is shown in Figure
4051 \ref{fig:kant-quot}.
4052 \end{mydef}
4053 We $\boxed{\text{box}}$ the neutral proofs and
4054 neutral members of empty types, following the notation in
4055 \cite{Altenkirch2007}, and we make use of $\mydefeq_{\mybox}$ which
4056 compares terms syntactically up to $\alpha$-renaming, but also up to
4057 equivalent proofs: we consider all boxed content as equal.
4058
4059 Our quotation will work on normalised terms, so that all defined values
4060 will have been replaced.  Moreover, we match on data type eliminators
4061 and all their arguments, so that $\mynat.\myfun{elim} \myappsp \mytmm
4062 \myappsp \myse{P} \myappsp \vec{\mytmn}$ will stand for
4063 $\mynat.\myfun{elim}$ applied to the scrutinised $\mynat$, the
4064 predicate, and the two cases.  This measure can be easily implemented by
4065 checking the head of applications and `consuming' the needed terms.
4066 Thus, we gain proof irrelevance, and not only for a more useful
4067 definitional equality, but also for example to eliminate all
4068 propositional content when compiling.
4069
4070 \begin{figure}[t]
4071   \mydesc{canonical quotation:}{\mycanquot(\myctx, \mytmsyn : \mytmsyn) \mymetagoes \mytmsyn}{
4072     \small
4073     $
4074     \begin{array}{@{}l@{}l}
4075       \mycanquot(\myctx,\ \mytmt : \mytyc{D} \myappsp \vec{A} &) \mymetaguard \mymeta{empty}(\myctx, \mytyc{D}) \mymetagoes \boxed{\mytmt} \\
4076       \mycanquot(\myctx,\ \mytmt : \mytyc{D} \myappsp \vec{A} &) \mymetaguard \mymeta{record}(\myctx, \mytyc{D}) \mymetagoes 
4077      \mytyc{D}.\mydc{constr} \myappsp \cdots \myappsp \mycanquot(\myctx, \mytyc{D}.\myfun{f}_n : (\myctx(\mytyc{D}.\myfun{f}_n))(\vec{A};\mytmt)) \\
4078       \mycanquot(\myctx,\ \mytyc{D}.\mydc{c} \myappsp \vec{t} : \mytyc{D} \myappsp \vec{A} &) \mymetagoes \cdots \\
4079       \mycanquot(\myctx,\ \myse{f} : \myfora{\myb{x}}{\mytya}{\mytyb} &) \mymetagoes \myabs{\myb{x}}{\mycanquot(\myctx; \myb{x} : \mytya, \myapp{\myse{f}}{\myb{x}} : \mytyb)} \\
4080       \mycanquot(\myctx,\ \myse{p} : \myprdec{\myse{P}} &) \mymetagoes \boxed{\myse{p}}
4081      \\
4082     \mycanquot(\myctx,\ \mytmt : \mytya &) \mymetagoes \mytmt'\ \text{\textbf{where}}\ \mytmt' : \myarg = \myneuquot(\myctx, \mytmt)
4083     \end{array}
4084     $
4085   }
4086
4087   \mynegder
4088
4089   \mydesc{neutral quotation:}{\myneuquot(\myctx, \mytmsyn) \mymetagoes \mytmsyn : \mytmsyn}{
4090     \small
4091     $
4092     \begin{array}{@{}l@{}l}
4093       \myneuquot(\myctx,\ \myb{x} &) \mymetagoes \myb{x} : \myctx(\myb{x}) \\
4094       \myneuquot(\myctx,\ \mytyp  &) \mymetagoes \mytyp : \mytyp \\
4095       \myneuquot(\myctx,\ \myfora{\myb{x}}{\mytya}{\mytyb} & ) \mymetagoes
4096        \myfora{\myb{x}}{\myneuquot(\myctx, \mytya)}{\myneuquot(\myctx; \myb{x} : \mytya, \mytyb)} : \mytyp \\
4097       \myneuquot(\myctx,\ \mytyc{D} \myappsp \vec{A} &) \mymetagoes \mytyc{D} \myappsp \cdots \mycanquot(\myctx, \mymeta{head}((\myctx(\mytyc{D}))(\mytya_1 \cdots \mytya_{n-1}))) : \mytyp \\
4098       \myneuquot(\myctx,\ \myprdec{\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb}} &) \mymetagoes \\
4099       \multicolumn{2}{l}{\myind{2}\myprdec{\myjm{\mycanquot(\myctx, \mytmm : \mytya)}{\mytya'}{\mycanquot(\myctx, \mytmn : \mytyb)}{\mytyb'}} : \mytyp} \\
4100       \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytya' : \myarg = \myneuquot(\myctx, \mytya)} \\
4101       \multicolumn{2}{@{}l}{\myind{2}\phantom{\text{\textbf{where}}}\ \mytyb' : \myarg = \myneuquot(\myctx, \mytyb)} \\
4102       \myneuquot(\myctx,\ \mytyc{D}.\myfun{f} \myappsp \mytmt &) \mymetaguard \mymeta{record}(\myctx, \mytyc{D}) \mymetagoes \mytyc{D}.\myfun{f} \myappsp \mytmt' : (\myctx(\mytyc{D}.\myfun{f}))(\vec{A};\mytmt) \\
4103       \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytmt' : \mytyc{D} \myappsp \vec{A} = \myneuquot(\myctx, \mytmt)} \\
4104       \myneuquot(\myctx,\ \mytyc{D}.\myfun{elim} \myappsp \mytmt \myappsp \myse{P} &) \mymetaguard \mymeta{empty}(\myctx, \mytyc{D}) \mymetagoes \mytyc{D}.\myfun{elim} \myappsp \boxed{\mytmt} \myappsp \myneuquot(\myctx, \myse{P}) : \myse{P} \myappsp \mytmt \\
4105       \myneuquot(\myctx,\ \mytyc{D}.\myfun{elim} \myappsp \mytmm \myappsp \myse{P} \myappsp \vec{\mytmn} &) \mymetagoes \mytyc{D}.\myfun{elim} \myappsp \mytmm' \myappsp \myneuquot(\myctx, \myse{P}) \cdots : \myse{P} \myappsp \mytmm\\
4106       \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytmm' : \mytyc{D} \myappsp \vec{A} = \myneuquot(\myctx, \mytmm)} \\
4107       \myneuquot(\myctx,\ \myapp{\myse{f}}{\mytmt} &) \mymetagoes \myapp{\myse{f'}}{\mycanquot(\myctx, \mytmt : \mytya)} : \mysub{\mytyb}{\myb{x}}{\mytmt} \\
4108       \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \myse{f'} : \myfora{\myb{x}}{\mytya}{\mytyb} = \myneuquot(\myctx, \myse{f})} \\
4109        \myneuquot(\myctx,\ \mycoee{\mytya}{\mytyb}{\myse{Q}}{\mytmt} &) \mymetaguard \myneuquot(\myctx, \mytya) \mydefeq_{\mybox} \myneuquot(\myctx, \mytyb) \mymetagoes \myneuquot(\myctx, \mytmt) \\
4110 \myneuquot(\myctx,\ \mycoee{\mytya}{\mytyb}{\myse{Q}}{\mytmt} &) \mymetagoes
4111        \mycoee{\myneuquot(\myctx, \mytya)}{\myneuquot(\myctx, \mytyb)}{\boxed{\myse{Q}}}{\myneuquot(\myctx, \mytmt)}
4112     \end{array}
4113     $
4114   }
4115   \caption{Quotation in \mykant.  Along the already used
4116     $\mymeta{record}$ meta-operation on the context we make use of
4117     $\mymeta{empty}$, which checks if a certain type constructor has
4118     zero data constructor.  The `data constructor' cases for non-record,
4119     non-empty, data types are omitted for brevity.}
4120   \label{fig:kant-quot}
4121 \end{figure}
4122
4123 \subsubsection{Why $\myprop$?}
4124
4125 It is worth to ask if $\myprop$ is needed at all.  It is perfectly
4126 possible to have the type checker identify propositional types
4127 automatically, and in fact in some sense we already do during equality
4128 reduction and quotation.  However, this has the considerable
4129 disadvantage that we can never identify abstracted
4130 variables\footnote{And in general neutral terms, although we currently
4131   do not have neutral propositions apart from equalities on neutral
4132   terms.} of type $\mytyp$ as $\myprop$, thus forbidding the user to
4133 talk about $\myprop$ explicitly.
4134
4135 This is a considerable impediment, for example when implementing
4136 \emph{quotient types}.  With quotients, we let the user specify an
4137 equivalence class over a certain type, and then exploit this in various
4138 way---crucially, we need to be sure that the equivalence given is
4139 propositional, a fact which prevented the use of quotients in dependent
4140 type theories \citep{Jacobs1994}.
4141
4142 \section{\mykant : the practice}
4143 \label{sec:kant-practice}
4144
4145 \epigraph{\emph{It's alive!}}{Henry Frankenstein}
4146
4147 The codebase consists of around 2500 lines of Haskell,\footnote{The full
4148   source code is available under the GPL3 license at
4149   \url{https://github.com/bitonic/kant}.  `Kant' was a previous
4150   incarnation of the software, and the name remained.} as reported by
4151 the \texttt{cloc} utility.
4152
4153 We implement the type theory as described in Section
4154 \ref{sec:kant-theory}.  The author learnt the hard way the
4155 implementation challenges for such a project, and ran out of time while
4156 implementing observational equality.  While the constructs and typing
4157 rules are present, the machinery to make it happen (equality reduction,
4158 coercions, quotation, etc.) is not present yet.
4159
4160 This considered, everything else presented in Section
4161 \ref{sec:kant-theory} is implemented and working well---and in fact all
4162 the examples presented in this thesis, apart from the ones that are
4163 equality related, have been encoded in \mykant\ in the Appendix.
4164 Moreover, given the detailed plan in the previous section, finishing off
4165 should not prove too much work.
4166
4167 The interaction with the user takes place in a loop living in and
4168 updating a context of \mykant\ declarations, which presents itself as in
4169 Figure \ref{fig:kant-web}.  Files with lists of declarations can also be
4170 loaded. The REPL is a available both as a command-line application and in
4171 a web interface, which is available at \url{bertus.mazzo.li}.
4172
4173 A REPL cycle starts with the user inputting a \mykant\
4174 declaration or another REPL command, which then goes through various
4175 stages that can end up in a context update, or in failures of various
4176 kind.  The process is described diagrammatically in figure
4177 \ref{fig:kant-process}.
4178
4179 \begin{figure}[b!]
4180 {\small\begin{Verbatim}[frame=leftline,xleftmargin=3cm]
4181 B E R T U S
4182 Version 0.0, made in London, year 2013.
4183 >>> :h
4184 <decl>     Declare value/data type/record
4185 :t <term>  Typecheck
4186 :e <term>  Normalise
4187 :p <term>  Pretty print
4188 :l <file>  Load file
4189 :r <file>  Reload file (erases previous environment)
4190 :i <name>  Info about an identifier
4191 :q         Quit
4192 >>> :l data/samples/good/common.ka 
4193 OK
4194 >>> :e plus three two
4195 suc (suc (suc (suc (suc zero))))
4196 >>> :t plus three two
4197 Type: Nat
4198 \end{Verbatim}
4199 }
4200
4201   \caption{A sample run of the \mykant\ prompt.}
4202   \label{fig:kant-web}
4203 \end{figure}
4204
4205
4206 \begin{description}
4207
4208 \item[Parse] In this phase the text input gets converted to a sugared
4209   version of the core language.  For example, we accept multiple
4210   arguments in arrow types and abstractions, and we represent variables
4211   with names, while as we will see in Section \ref{sec:term-repr} the
4212   final term types uses a \emph{nameless} representation.
4213
4214 \item[Desugar] The sugared declaration is converted to a core term.
4215   Most notably we go from names to nameless.
4216
4217 \item[ConDestr] Short for `Constructors/Destructors', converts
4218   applications of data destructors and constructors to a special form,
4219   to perform bidirectional type checking.
4220
4221 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
4222   which is necessary to implement the type hierarchy check.
4223
4224 \item[Elaborate/Typecheck/Evaluate] \textbf{Elaboration} converts the
4225   declaration to some context items, which might be a value declaration
4226   (type and body) or a data type declaration (constructors and
4227   destructors).  This phase works in tandem with \textbf{Type checking},
4228   which in turns needs to \textbf{Evaluate} terms.
4229
4230 \item[Distill] and report the result.  `Distilling' refers to the
4231   process of converting a core term back to a sugared version that we
4232   can show to the user.  This can be necessary both to display errors
4233   including terms or to display result of evaluations or type checking
4234   that the user has requested.  Among the other things in this stage we
4235   go from nameless back to names by recycling the names that the user
4236   used originally, as to fabricate a term which is as close as possible
4237   to what it originated from.
4238
4239 \item[Pretty print] Format the terms in a nice way, and display them to
4240   the user.
4241
4242 \end{description}
4243
4244 \begin{figure}
4245   \centering{\mysmall
4246     \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
4247     corners, minimum height=2.5em, node distance=0.7cm]
4248       
4249       \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
4250       centered, inner sep=0pt, node distance=0.7cm]
4251       
4252       \tikzstyle{line} = [draw, -latex']
4253       
4254       \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
4255       centered, node distance=1.5cm]
4256       
4257       
4258       \begin{tikzpicture}[auto]
4259         \node [cloud] (user) {User};
4260         \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
4261         \node [block, below=of parse] (desugar) {Desugar};
4262         \node [block, below=of desugar] (condestr) {ConDestr};
4263         \node [block, below=of condestr] (reference) {Reference};
4264         \node [block, below=of reference] (elaborate) {Elaborate};
4265         \node [block, left=of elaborate] (tycheck) {Typecheck};
4266         \node [block, left=of tycheck] (evaluate) {Evaluate};
4267         \node [decision, right=of elaborate] (error) {Error?};
4268         \node [block, right=of parse] (pretty) {Pretty print};
4269         \node [block, below=of pretty] (distill) {Distill};
4270         \node [block, below=of distill] (update) {Update context};
4271         
4272         \path [line] (user) -- (parse);
4273         \path [line] (parse) -- (desugar);
4274         \path [line] (desugar) -- (condestr);
4275         \path [line] (condestr) -- (reference);
4276         \path [line] (reference) -- (elaborate);
4277         \path [line] (elaborate) edge[bend right] (tycheck);
4278         \path [line] (tycheck) edge[bend right] (elaborate);
4279         \path [line] (elaborate) -- (error);
4280         \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
4281         \path [line] (error) -- node [near start] {no} (update);
4282         \path [line] (update) -- (distill);
4283         \path [line] (pretty) -- (user);
4284         \path [line] (distill) -- (pretty);
4285         \path [line] (tycheck) edge[bend right] (evaluate);
4286         \path [line] (evaluate) edge[bend right] (tycheck);
4287       \end{tikzpicture}
4288   }
4289   \caption{High level overview of the life of a \mykant\ prompt cycle.}
4290   \label{fig:kant-process}
4291 \end{figure}
4292
4293 Here we will review only a sampling of the more interesting
4294 implementation challenges present when implementing an interactive
4295 theorem prover.
4296
4297 \subsection{Syntax}
4298
4299 The syntax of \mykant\ is presented in Figure \ref{fig:syntax}.
4300 Examples showing the usage of most of the constructs---excluding the
4301 OTT-related ones---are present in Appendices \ref{app:kant-itt},
4302 \ref{app:kant-examples}, and \ref{app:hurkens}; plus a tutorial in
4303 Section \ref{sec:type-holes}.  The syntax has grown organically with the
4304 needs of the language, and thus is not very sophisticated.  The grammar
4305 is specified in and processed by the \texttt{happy} parser generator for
4306 Haskell.\footnote{Available at \url{http://www.haskell.org/happy}.}
4307 Tokenisation is performed by a simple hand written lexer.
4308
4309 \begin{figure}[p]
4310   \centering
4311   $
4312   \begin{array}{@{\ \ }l@{\ }c@{\ }l}
4313     \multicolumn{3}{@{}l}{\text{A name, in regexp notation.}} \\
4314     \mysee{name}   & ::= & \texttt{[a-zA-Z] [a-zA-Z0-9'\_-]*} \\
4315     \multicolumn{3}{@{}l}{\text{A binder might or might not (\texttt{\_}) bind a name.}} \\
4316     \mysee{binder} & ::= & \mytermi{\_} \mysynsep \mysee{name} \\
4317     \multicolumn{3}{@{}l}{\text{A series of typed bindings.}} \\
4318     \mysee{telescope}\, \ \ \  & ::= & (\mytermi{[}\ \mysee{binder}\ \mytermi{:}\ \mysee{term}\ \mytermi{]}){*} \\
4319     \multicolumn{3}{@{}l}{\text{Terms, including propositions.}} \\
4320     \multicolumn{3}{@{}l}{
4321       \begin{array}{@{\ \ }l@{\ }c@{\ }l@{\ \ \ \ \ }l}
4322     \mysee{term} & ::= & \mysee{name} & \text{A variable.} \\
4323                  &  |  & \mytermi{*}  & \text{\mytyc{Type}.} \\
4324                  &  |  & \mytermi{\{|}\ \mysee{term}{*}\ \mytermi{|\}} & \text{Type holes.} \\
4325                  &  |  & \mytermi{Prop} & \text{\mytyc{Prop}.} \\
4326                  &  |  & \mytermi{Top} \mysynsep \mytermi{Bot} & \text{$\mytop$ and $\mybot$.} \\
4327                  &  |  & \mysee{term}\ \mytermi{/\textbackslash}\ \mysee{term} & \text{Conjuctions.} \\
4328                  &  |  & \mytermi{[|}\ \mysee{term}\ \mytermi{|]} & \text{Proposition decoding.} \\
4329                  &  |  & \mytermi{coe}\ \mysee{term}\ \mysee{term}\ \mysee{term}\ \mysee{term} & \text{Coercion.} \\
4330                  &  |  & \mytermi{coh}\ \mysee{term}\ \mysee{term}\ \mysee{term}\ \mysee{term} & \text{Coherence.} \\
4331                  &  | & \mytermi{(}\ \mysee{term}\ \mytermi{:}\ \mysee{term}\ \mytermi{)}\ \mytermi{=}\ \mytermi{(}\ \mysee{term}\ \mytermi{:}\ \mysee{term}\ \mytermi{)} & \text{Heterogeneous equality.} \\
4332                  &  |  & \mytermi{(}\ \mysee{compound}\ \mytermi{)} & \text{Parenthesised term.} \\
4333       \mysee{compound} & ::= & \mytermi{\textbackslash}\ \mysee{binder}{*}\ \mytermi{=>}\ \mysee{term} & \text{Untyped abstraction.} \\
4334                        &  |  & \mytermi{\textbackslash}\ \mysee{telescope}\ \mytermi{:}\ \mysee{term}\ \mytermi{=>}\ \mysee{term} & \text{Typed abstraction.} \\
4335                  &  | & \mytermi{forall}\ \mysee{telescope}\ \mysee{term} & \text{Universal quantification.} \\
4336                  &  | & \mysee{arr} \\
4337        \mysee{arr}    & ::= & \mysee{telescope}\ \mytermi{->}\ \mysee{arr} & \text{Dependent function.} \\
4338                       &  |  & \mysee{term}\ \mytermi{->}\ \mysee{arr} & \text{Non-dependent function.} \\
4339                       &  |  & \mysee{term}{+} & \text {Application.}
4340       \end{array}
4341     } \\
4342     \multicolumn{3}{@{}l}{\text{Typed names.}} \\
4343     \mysee{typed} & ::= & \mysee{name}\ \mytermi{:}\ \mysee{term} \\
4344     \multicolumn{3}{@{}l}{\text{Declarations.}} \\
4345     \mysee{decl}& ::= & \mysee{value} \mysynsep \mysee{abstract} \mysynsep \mysee{data} \mysynsep \mysee{record} \\
4346     \multicolumn{3}{@{}l}{\text{Defined values.  The telescope specifies named arguments.}} \\
4347     \mysee{value} & ::= & \mysee{name}\ \mysee{telescope}\ \mytermi{:}\ \mysee{term}\ \mytermi{=>}\ \mysee{term} \\
4348     \multicolumn{3}{@{}l}{\text{Abstracted variables.}} \\
4349     \mysee{abstract} & ::= & \mytermi{postulate}\ \mysee{typed} \\
4350     \multicolumn{3}{@{}l}{\text{Data types, and their constructors.}} \\
4351     \mysee{data} & ::= & \mytermi{data}\ \mysee{name}\ \mysee{telescope}\ \mytermi{->}\ \mytermi{*}\ \mytermi{=>}\ \mytermi{\{}\ \mysee{constrs}\ \mytermi{\}} \\
4352     \mysee{constrs} & ::= & \mysee{typed} \\
4353                    &  |  & \mysee{typed}\ \mytermi{|}\ \mysee{constrs} \\
4354     \multicolumn{3}{@{}l}{\text{Records, and their projections.  The $\mysee{name}$ before the projections is the constructor name.}} \\
4355     \mysee{record} & ::= & \mytermi{record}\ \mysee{name}\ \mysee{telescope}\ \mytermi{->}\ \mytermi{*}\ \mytermi{=>}\ \mysee{name}\ \mytermi{\{}\ \mysee{projs}\ \mytermi{\}} \\
4356     \mysee{projs} & ::= & \mysee{typed} \\
4357                    &  |  & \mysee{typed}\ \mytermi{,}\ \mysee{projs}
4358   \end{array}
4359   $
4360
4361   \caption{\mykant' syntax.  The non-terminals are marked with
4362     $\langle\text{angle brackets}\rangle$ for greater clarity.  The
4363     syntax in the implementation is actually more liberal, for example
4364     giving the possibility of using arrow types directly in
4365     constructor/projection declarations.\\
4366     Additionally, we give the user the possibility of using Unicode
4367     characters instead of their ASCII counterparts, e.g. \texttt{→} in
4368     place of \texttt{->}, \texttt{λ} in place of
4369     \texttt{\textbackslash}, etc.}
4370   \label{fig:syntax}
4371 \end{figure}
4372
4373 \subsection{Term representation}
4374 \label{sec:term-repr}
4375
4376 \subsubsection{Naming and substituting}
4377
4378 Perhaps surprisingly, one of the most difficult challenges in
4379 implementing a theory of the kind presented is choosing a good data type
4380 for terms, and specifically handling substitutions in a sane way.
4381
4382 There are two broad schools of thought when it comes to naming
4383 variables, and thus substituting:
4384 \begin{description}
4385 \item[Nameful] Bound variables are represented by some enumerable data
4386   type, just as we have described up to now, starting from Section
4387   \ref{sec:untyped}.  The problem is that avoiding name capturing is a
4388   nightmare, both in the sense that it is not performant---given that we
4389   need to rename rename substitute each time we `enter' a binder---but
4390   most importantly given the fact that in even slightly more complicated
4391   systems it is very hard to get right, even for experts.
4392
4393   One of the sore spots of explicit names is comparing terms up to
4394   $\alpha$-renaming, which again generates a huge amounts of
4395   substitutions and requires special care.  
4396
4397 \item[Nameless] We can capture the relationship between variables and
4398   their binders, by getting rid of names altogether, and representing
4399   bound variables with an index referring to the `binding' structure, a
4400   notion introduced by \cite{de1972lambda}.  Usually $0$ represents the
4401   variable bound by the innermost binding structure, $1$ the
4402   second-innermost, and so on.  For instance with simple abstractions we
4403   might have
4404   \[
4405   \begin{array}{@{}l}
4406   \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\text{, which corresponds to} \\
4407   \myabs{\myb{f}}{(\myabs{\myb{g}}{\myapp{\myb{g}}{(\myabs{\myb{x}}{\myb{x}})}}) \myappsp (\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}})} : ((\mytya \myarr \mytya) \myarr \mytyb) \myarr \mytyb
4408   \end{array}
4409   \]
4410
4411   While this technique is obviously terrible in terms of human
4412   usability,\footnote{With some people going as far as defining it akin
4413   to an inverse Turing test.} it is much more convenient as an
4414   internal representation to deal with terms mechanically---at least in
4415   simple cases.  $\alpha$-renaming ceases to be an issue, and
4416   term comparison is purely syntactical.
4417
4418   Nonetheless, more complex constructs such as pattern matching put
4419   some strain on the indices and many systems end up using explicit
4420   names anyway.
4421
4422 \end{description}
4423
4424 In the past decade or so advancements in the Haskell's type system and
4425 in general the spread new programming practices have made the nameless
4426 option much more amenable.  \mykant\ thus takes the nameless path
4427 through the use of Edward Kmett's excellent \texttt{bound}
4428 library.\footnote{Available at
4429   \url{http://hackage.haskell.org/package/bound}.}  We describe the
4430 advantages of \texttt{bound}'s approach, but also its pitfalls in the
4431 previously relatively unknown territory of dependent
4432 types---\texttt{bound} being created mostly to handle more simply typed
4433 systems.
4434
4435   \texttt{bound} builds on the work of \cite{Bird1999}, who suggested to
4436   parametrising the term type over the type of the variables, and `nest'
4437   the type each time we enter a scope.  If we wanted to define a term
4438   for the untyped $\lambda$-calculus, we might have
4439 \begin{Verbatim}
4440 -- A type with no members.
4441 data Empty
4442
4443 data Var v = Bound | Free v
4444
4445 data Tm v
4446     = V v               -- Bound variable
4447     | App (Tm v) (Tm v) -- Term application
4448     | Lam (Tm (Var v))  -- Abstraction
4449 \end{Verbatim}
4450 Closed terms would be of type \texttt{Tm Empty}, so that there would be
4451 no occurrences of \texttt{V}.  However, inside an abstraction, we can
4452 have \texttt{V Bound}, representing the bound variable, and inside a
4453 second abstraction we can have \texttt{V Bound} or \texttt{V (Free
4454 Bound)}.  Thus the term
4455 \[\myabs{\myb{x}}{\myabs{\myb{y}}{\myb{x}}}\]
4456 can be represented as
4457 \begin{Verbatim}
4458 -- The top level term is of type `Tm Empty'.
4459 -- The inner term `Lam (Free Bound)' is of type `Tm (Var Empty)'.
4460 -- The second inner term `V (Free Bound)' is of type `Tm (Var (Var
4461 -- Empty))'.
4462 Lam (Lam (V (Free Bound)))
4463 \end{Verbatim}
4464 This allows us to reflect the `nestedness' of a type at the type level,
4465 and since we usually work with functions polymorphic on the parameter
4466 \texttt{v} it's very hard to make mistakes by putting terms of the wrong
4467 nestedness where they do not belong.
4468
4469 Even more interestingly, the substitution operation is perfectly
4470 captured by the \verb|>>=| (bind) operator of the \texttt{Monad}
4471 type class:
4472 \begin{Verbatim}
4473 class Monad m where
4474   return :: m a
4475   (>>=)  :: m a -> (a -> m b) -> m b
4476
4477 instance Monad Tm where
4478   -- `return'ing turns a variable into a `Tm'
4479   return = V
4480
4481   -- `t >>= f' takes a term `t' and a mapping from variables to terms
4482   -- `f' and applies `f' to all the variables in `t', replacing them
4483   -- with the mapped terms.
4484   V v     >>= f = f v
4485   App m n >>= f = App (m >>= f) (n >>= f)
4486
4487   -- `Lam' is the tricky case: we modify the function to work with bound
4488   -- variables, so that if it encounters `Bound' it leaves it untouched
4489   -- (since the mapping refers to the outer scope); if it encounters a
4490   -- free variable it asks `f' for the term and then updates all the
4491   -- variables to make them refer to the outer scope they were meant to
4492   -- be in.
4493   Lam s   >>= f = Lam (s >>= bump)
4494     where bump Bound    = return Bound
4495           bump (Free v) = f v >>= V . Free
4496 \end{Verbatim}
4497 With this in mind, we can define functions which will not only work on
4498 \verb|Tm|, but on any \verb|Monad|!
4499 \begin{Verbatim}
4500 -- Replaces free variable `v' with `m' in `n'.
4501 subst :: (Eq v, Monad m) => v -> m v -> m v -> m v
4502 subst v m n = n >>= \v' -> if v == v' then m else return v'
4503
4504 -- Replace the variable bound by `s' with term `t'.
4505 inst :: Monad m => m v -> m (Var v) -> m v
4506 inst t s = s >>= \v -> case v of
4507                            Bound   -> t
4508                            Free v' -> return v'
4509 \end{Verbatim}
4510 The beauty of this technique is that with a few simple functions we have
4511 defined all the core operations in a general and `obviously correct'
4512 way, with the extra confidence of having the type checker looking our
4513 back.  For what concerns term equality, we can just ask the Haskell
4514 compiler to derive the instance for the \verb|Eq| type class and since
4515 we are nameless that will be enough (modulo fancy quotation).
4516
4517 Moreover, if we take the top level term type to be \verb|Tm String|, we
4518 get a representation of terms with top-level definitions; where closed
4519 terms contain only \verb|String| references to said definitions---see
4520 also \cite{McBride2004b}.
4521
4522 What are then the pitfalls of this seemingly invincible technique?  The
4523 most obvious impediment is the need for polymorphic recursion.
4524 Functions traversing terms parametrised by the variable type will have
4525 types such as
4526 \begin{Verbatim}
4527 -- Infer the type of a term, or return an error.
4528 infer :: Tm v -> Either Error (Tm v)
4529 \end{Verbatim}
4530 When traversing under a \verb|Scope| the parameter changes from \verb|v|
4531 to \verb|Var v|, and thus if we do not specify the type for our function explicitly
4532 inference will fail---type inference for polymorphic recursion being
4533 undecidable \citep{henglein1993type}.  This causes some annoyance,
4534 especially in the presence of many local definitions that we would like
4535 to leave untyped.
4536
4537 But the real issue is the fact that giving a type parametrised over a
4538 variable---say \verb|m v|---a \verb|Monad| instance means being able to
4539 only substitute variables for values of type \verb|m v|.  This is a
4540 considerable inconvenience.  Consider for instance the case of
4541 telescopes, which are a central tool to deal with contexts and other
4542 constructs.  In Haskell we can give them a faithful representation
4543 with a data type along the lines of
4544 \begin{Verbatim}
4545 data Tele m v = Empty (m v) | Bind (m v) (Tele m (Var v))
4546 type TeleTm = Tele Tm
4547 \end{Verbatim}
4548 The problem here is that what we want to substitute for variables in
4549 \verb|Tele m v| is \verb|m v| (probably \verb|Tm v|), not \verb|Tele m v| itself!  What we need is
4550 \begin{Verbatim}
4551 bindTele  :: Monad m => Tele m a -> (a -> m b) -> Tele m b
4552 substTele :: (Eq v, Monad m) => v -> m v -> Tele m v -> Tele m v
4553 instTele  :: Monad m => m v -> Tele m (Var v) -> Tele m v
4554 \end{Verbatim}
4555 Not what \verb|Monad| gives us.  Solving this issue in an elegant way
4556 has been a major sink of time and source of headaches for the author,
4557 who analysed some of the alternatives---most notably the work by
4558 \cite{weirich2011binders}---but found it impossible to give up the
4559 simplicity of the model above.
4560
4561 That said, our term type is still reasonably brief, as shown in full in
4562 Appendix \ref{app:termrep}.  The fact that propositions cannot be
4563 factored out in another data type is an instance of the problem
4564 described above.  However the real pain is during elaboration, where we
4565 are forced to treat everything as a type while we would much rather have
4566 telescopes.  Future work would include writing a library that marries
4567 more flexibility with a nice interface similar to the one of
4568 \verb|bound|.
4569
4570 We also make use of a `forgetful' data type (as provided by
4571 \verb|bound|) to store user-provided variables names along with the
4572 `nameless' representation, so that the names will not be considered when
4573 compared terms, but will be available when distilling so that we can
4574 recover variable names that are as close as possible to what the user
4575 originally used.
4576
4577 \subsubsection{Evaluation}
4578
4579 Another source of contention related to term representation is dealing
4580 with evaluation.  Here \mykant\ does not make bold moves, and simply
4581 employs substitution.  When type checking we match types by reducing
4582 them to their weak head normal form, as to avoid unnecessary evaluation.
4583
4584 We treat data types eliminators and record projections in an uniform
4585 way, by elaborating declarations in a series of \emph{rewriting rules}:
4586 \begin{Verbatim}
4587 type Rewr =
4588     forall v.
4589     Tm v   ->    -- Term to which the destructor is applied
4590     [Tm v] ->    -- List of other arguments
4591     -- The result of the rewriting, if the eliminator reduces.
4592     Maybe [Tm v]
4593 \end{Verbatim}
4594 A rewriting rule is polymorphic in the variable type, guaranteeing that
4595 it just pattern matches on terms structure and rearranges them in some
4596 way, and making it possible to apply it at any level in the term.  When
4597 reducing a series of applications we match the first term and check if
4598 it is a destructor, and if that's the case we apply the reduction rule
4599 and reduce further if it yields a new list of terms.
4600
4601 This has the advantage of simplicity, at the expense of being quite poor
4602 in terms of performance and that we need to do quotation manually.  An
4603 alternative that solves both of these is the already mentioned
4604 \emph{normalization by evaluation}, where we would compute by turning
4605 terms into Haskell values, and then reify back to terms to compare
4606 them---a useful tutorial on this technique is given by \cite{Loh2010}.
4607
4608 However, quotation has its disadvantages.  The most obvious one is that
4609 it is less simple: we need to set up some infrastructure to handle the
4610 quotation and reification, while with substitution we have a uniform
4611 representation through the process of type checking.  The second is that
4612 performance advantages can be rendered less effective by the continuous
4613 quoting and reifying, although this can probably be mitigated with some
4614 heuristics.
4615
4616 \subsubsection{Parametrise everything!}
4617 \label{sec:parame}
4618
4619 Through the life of a REPL cycle we need to execute two broad
4620 `effectful' actions:
4621 \begin{itemize}
4622 \item Retrieve, add, and modify elements to an environment.  The
4623   environment will contain not only types, but also the rewriting rules
4624   presented in the previous section, and a counter to generate fresh
4625   references for the type hierarchy.
4626
4627 \item Throw various kinds of errors when something goes wrong: parsing,
4628   type checking, input/output error when reading files, and more.
4629 \end{itemize}
4630 Haskell taught us the value of monads in programming languages, and in
4631 \mykant\ we keep this lesson in mind.  All of the plumbing required to do
4632 the two actions above is provided by a very general \emph{monad
4633   transformer} that we use through the codebase, \texttt{KMonadT}:
4634 \begin{Verbatim}
4635 newtype KMonad f v m a = KMonad (StateT (f v) (ErrorT KError m) a)
4636
4637 data KError
4638     = OutOfBounds Id
4639     | DuplicateName Id
4640     | IOError IOError
4641     | ...
4642 \end{Verbatim}
4643 Without delving into the details of what a monad transformer
4644 is,\footnote{See
4645   \url{https://en.wikibooks.org/wiki/Haskell/Monad_transformers.}} this
4646 is what \texttt{KMonadT} works with and provides:
4647 \begin{itemize}
4648 \item The \verb|v| parameter represents the parametrised variable for
4649   the term type that we spoke about at the beginning of this section.
4650   More on this later.
4651
4652 \item The \verb|f| parameter indicates what kind of environment we are
4653   holding.  Sometimes we want to traverse terms without carrying the
4654   entire environment, for various reasons---\texttt{KMonatT} lets us do
4655   that.  Note that \verb|f| is itself parametrised over \verb|v|.  The
4656   inner \verb|StateT| monad transformer lets us retrieve and modify this
4657   environment at any time.
4658
4659 \item The \verb|m| is the `inner' monad that we can `plug in' to be able
4660   to perform more effectful actions in \texttt{KMonatT}.  For example if we
4661   plug the \texttt{IO} monad in, we will be able to do input/output.
4662
4663 \item The inner \verb|ErrorT| lets us throw errors at any time.  The
4664   error type is \verb|KError|, which describes all the possible errors
4665   that a \mykant\ process can throw.
4666
4667 \item Finally, the \verb|a| parameter represents the return type of the
4668   computation we are executing.
4669 \end{itemize}
4670
4671 The clever trick in \texttt{KMonadT} is to have it to be parametrised
4672 over the same type as the term type.  This way, we can easily carry the
4673 environment while traversing under binders.  For example, if we only
4674 needed to carry types of bound variables in the environment, we can
4675 quickly set up the following infrastructure:
4676 \begin{Verbatim}
4677 data Tm v = ...
4678
4679 -- A context is a mapping from variables to types.
4680 newtype Ctx v = Ctx (v -> Tm v)
4681
4682 -- A context monad holds a context.
4683 type CtxMonad v m = KMonadT Ctx v m
4684
4685 -- Enter into a scope binding a type to the variable, execute a
4686 -- computation there, and return exit the scope returning to the `current'
4687 -- context.
4688 nestM :: Monad m => Tm v -> CtxMonad (Var v) m a -> CtxMonad v m a
4689 nestM = ...
4690 \end{Verbatim}
4691 Again, the types guard our back guaranteeing that we add a type when we
4692 enter a scope, and we discharge it when we get out.  The author
4693 originally started with a more traditional representation and often
4694 forgot to add the right variable at the right moment.  Using this
4695 practices it is very difficult to do so---we achieve correctness through
4696 types.
4697
4698 In the actual \mykant\ codebase, we have also abstracted the concept of
4699 `context' further, so that we can easily embed contexts into other
4700 structures and write generic operations on all context-like
4701 structures.\footnote{See the \texttt{Kant.Cursor} module for details.}
4702
4703 \subsection{Turning a hierarchy into some graphs}
4704 \label{sec:hier-impl}
4705
4706 In this section we will explain how to implement the typical ambiguity
4707 we have spoken about in \ref{sec:term-hierarchy} efficiently, a subject
4708 which is often dismissed in the literature.  As mentioned, we have to
4709 verify a the consistency of a set of constraints each time we add a new
4710 one.  The constraints range over some set of variables whose members we
4711 will denote with $x, y, z, \dots$.  and are of two kinds:
4712 \begin{center}
4713   \begin{tabular}{cc}
4714      $x \le y$ & $x < y$
4715   \end{tabular}
4716 \end{center}
4717
4718 Predictably, $\le$ expresses a reflexive order, and $<$ expresses an
4719 irreflexive order, both working with the same notion of equality, where
4720 $x < y$ implies $x \le y$---they behave like $\le$ and $<$ do for natural
4721 numbers (or in our case, levels in a type hierarchy).  We also need an
4722 equality constraint ($x = y$), which can be reduced to two constraints
4723 $x \le y$ and $y \le x$.
4724
4725 Given this specification, we have implemented a standalone Haskell
4726 module---that we plan to release as a library---to efficiently store and
4727 check the consistency of constraints.  The problem predictably reduces
4728 to a graph algorithm, and for this reason we also implement a library
4729 for labelled graphs, since the existing Haskell graph libraries fell
4730 short in different areas.\footnote{We tried the \texttt{Data.Graph}
4731   module in \url{http://hackage.haskell.org/package/containers}, and the
4732   much more featureful \texttt{fgl} library
4733   \url{http://hackage.haskell.org/package/fgl}.}  The interfaces for
4734 these modules are shown in Appendix \ref{app:constraint}.  The graph
4735 library is implemented as a modification of the code described by
4736 \cite{King1995}.
4737
4738 We represent the set by building a graph where vertices are variables,
4739 and edges are constraints between them, labelled with the appropriate
4740 constraint: $x < y$ gives rise to a $<$-labelled edge from $x$ to $y$,
4741 and $x \le y$ to a $\le$-labelled edge from $x$ to $y$.  As we add
4742 constraints, $\le$ constraints are replaced by $<$ constraints, so that
4743 if we started with an empty set and added
4744 \[
4745    x < y,\ y \le z,\ z \le k,\ k < j,\ j \le y\
4746 \]
4747 it would generate the graph shown in Figure \ref{fig:graph-one-before},
4748 but adding $z < k$ would strengthen the edge from $z$ to $k$, as shown
4749 in \ref{fig:graph-one-after}.
4750
4751 \begin{figure}[t]
4752   \centering
4753   \begin{subfigure}[b]{0.3\textwidth}
4754     \begin{tikzpicture}[node distance=1.5cm]
4755       % Place nodes
4756       \node (x) {$x$};
4757       \node [right of=x] (y) {$y$};
4758       \node [right of=y] (z) {$z$};
4759       \node [below of=z] (k) {$k$};
4760       \node [left  of=k] (j) {$j$};
4761       %% Lines
4762       \path[->]
4763       (x) edge node [above] {$<$}   (y)
4764       (y) edge node [above] {$\le$} (z)
4765       (z) edge node [right] {$\le$}   (k)
4766       (k) edge node [below] {$\le$} (j)
4767       (j) edge node [left ] {$\le$} (y);
4768     \end{tikzpicture}
4769     \caption{Before $z < k$.}
4770     \label{fig:graph-one-before}
4771   \end{subfigure}%
4772   ~
4773   \begin{subfigure}[b]{0.3\textwidth}
4774     \begin{tikzpicture}[node distance=1.5cm]
4775       % Place nodes
4776       \node (x) {$x$};
4777       \node [right of=x] (y) {$y$};
4778       \node [right of=y] (z) {$z$};
4779       \node [below of=z] (k) {$k$};
4780       \node [left  of=k] (j) {$j$};
4781       %% Lines
4782       \path[->]
4783       (x) edge node [above] {$<$}   (y)
4784       (y) edge node [above] {$\le$} (z)
4785       (z) edge node [right] {$<$}   (k)
4786       (k) edge node [below] {$\le$} (j)
4787       (j) edge node [left ] {$\le$} (y);
4788     \end{tikzpicture}
4789     \caption{After $z < k$.}
4790     \label{fig:graph-one-after}
4791   \end{subfigure}%
4792   ~
4793   \begin{subfigure}[b]{0.3\textwidth}
4794     \begin{tikzpicture}[remember picture, node distance=1.5cm]
4795       \begin{pgfonlayer}{foreground}
4796       % Place nodes
4797       \node (x) {$x$};
4798       \node [right of=x] (y) {$y$};
4799       \node [right of=y] (z) {$z$};
4800       \node [below of=z] (k) {$k$};
4801       \node [left  of=k] (j) {$j$};
4802       %% Lines
4803       \path[->]
4804       (x) edge node [above] {$<$}   (y)
4805       (y) edge node [above] {$\le$} (z)
4806       (z) edge node [right] {$<$}   (k)
4807       (k) edge node [below] {$\le$} (j)
4808       (j) edge node [left ] {$\le$} (y);
4809     \end{pgfonlayer}{foreground}
4810     \end{tikzpicture}
4811     \begin{tikzpicture}[remember picture, overlay]
4812       \begin{pgfonlayer}{background}
4813       \fill [red, opacity=0.3, rounded corners]
4814       (-2.7,2.6) rectangle (-0.2,0.05)
4815       (-4.1,2.4) rectangle (-3.3,1.6);
4816     \end{pgfonlayer}{background}
4817     \end{tikzpicture}
4818     \caption{SCCs.}
4819     \label{fig:graph-one-scc}
4820   \end{subfigure}%
4821   \caption{Strong constraints overrule weak constraints.}
4822   \label{fig:graph-one}
4823 \end{figure}
4824
4825 \begin{mydef}[Strongly connected component]
4826   A \emph{strongly connected component} in a graph with vertices $V$ is
4827   a subset of $V$, say $V'$, such that for each $(v_1,v_2) \in V' \times
4828   V$ there is a path from $v_1$ to $v_2$.
4829 \end{mydef}
4830
4831 The SCCs in the graph for the constraints above is shown in Figure
4832 \ref{fig:graph-one-scc}.  If we have a strongly connected component with
4833 a $<$ edge---say $x < y$---in it, we have an inconsistency, since there
4834 must also be a path from $y$ to $x$, and by transitivity it must either
4835 be the case that $y \le x$ or $y < x$, which are both at odds with $x <
4836 y$.
4837
4838 Moreover, if we have a SCC with no $<$ edges, it means that all members
4839 of said SCC are equal, since for every $x \le y$ we have a path from $y$
4840 to $x$, which again by transitivity means that $y \le x$.  Thus, we can
4841 \emph{condense} the SCC to a single vertex, by choosing a variable among
4842 the SCC as a representative for all the others.  This can be done
4843 efficiently with disjoint set data structure, and is crucial to keep the
4844 graph compact, given the very large number of constraints generated when
4845 type checking.
4846
4847 \subsection{(Web) REPL}
4848
4849 Finally, we take a break from the types by giving a brief account of the
4850 design of our REPL, being a good example of modular design using various
4851 constructs dear to the Haskell programmer.
4852
4853 Keeping in mind the \texttt{KMonadT} monad described in Section
4854 \ref{sec:parame}, the REPL is represented as a function in
4855 \texttt{KMonadT} consuming input and hopefully producing output.  Then,
4856 frontends can very easily written by marshalling data in and out of the
4857 REPL:
4858 \begin{Verbatim}
4859 data Input
4860     = ITyCheck String           -- Type check a term
4861     | IEval String              -- Evaluate a term
4862     | IDecl String              -- Declare something
4863     | ...
4864
4865 data Output
4866     = OTyCheck TmRefId [HoleCtx] -- Type checked term, with holes
4867     | OPretty TmRefId            -- Term to pretty print, after evaluation
4868       -- Just holes, classically after loading a file
4869     | OHoles [HoleCtx]
4870     | ... 
4871     
4872 -- KMonadT is parametrised over the type of the variables, which depends
4873 -- on how deep in the term structure we are.  For the REPL, we only deal
4874 -- with top-level terms, and thus only `Id' variables---top level names.
4875 type REPL m = KMonadT Id m
4876
4877 repl :: ReadFile m => Input -> REPL m Output
4878 repl = ...
4879 \end{Verbatim}
4880 The \texttt{ReadFile} monad embodies the only `extra' action that we
4881 need to have access too when running the REPL: reading files.  We could
4882 simply use the \texttt{IO} monad, but this will not serve us well when
4883 implementing front end facing untrusted parties accessing the application
4884 running on our servers.  In our case we expose the REPL as a web
4885 application, and we want the user to be able to load only from a
4886 pre-defined directory, not from the entire file system.
4887
4888 For this reason we specify \texttt{ReadFile} to have just one function:
4889 \begin{Verbatim}
4890 class Monad m => ReadFile m where
4891     readFile' :: FilePath -> m (Either IOError String)
4892 \end{Verbatim}
4893 While in the command-line application we will use the \texttt{IO} monad
4894 and have \texttt{readFile'} to work in the `obvious' way---by reading
4895 the file corresponding to the given file path---in the web prompt we
4896 will have it to accept only a file name, not a path, and read it from a
4897 pre-defined directory:
4898 \begin{Verbatim}
4899 -- The monad that will run the web REPL.  The `ReaderT' holds the
4900 -- filepath to the directory where the files loadable by the user live.
4901 -- The underlying `IO' monad will be used to actually read the files.
4902 newtype DirRead a = DirRead (ReaderT FilePath IO a)
4903
4904 instance ReadFile DirRead where
4905     readFile' fp =
4906         do -- We get the base directory in the `ReaderT' with `ask'
4907            dir <- DirRead ask
4908            -- Is the filepath provided an unqualified file name?
4909            if snd (splitFileName fp) == fp
4910               -- If yes, go ahead and read the file, by lifting
4911               -- `readFile'' into the IO monad
4912               then DirRead (lift (readFile' (dir </> fp)))
4913               -- If not, return an error
4914               else return (Left (strMsg ("Invalid file name `" ++ fp ++ "'")))
4915 \end{Verbatim}
4916 Once this light-weight infrastructure is in place, adding a web
4917 interface was an easy exercise.  We use Jasper Van der Jeugt's
4918 \texttt{websockets} library\footnote{Available at
4919   \url{http://hackage.haskell.org/package/websockets}.} to create a
4920 proxy that receives \texttt{JSON}\footnote{\texttt{JSON} is a popular data interchange
4921   format, see \url{http://json.org} for more info.}  messages with the
4922 user input, turns them into \texttt{Input} messages for the REPL, and
4923 then sends back a \texttt{JSON} message with the response.  Moreover, each client
4924 is handled in a separate threads, so crashes of the REPL for a certain
4925 client will not bring the whole application down.
4926
4927 On the frontend side, we had to write some JavaScript to accept input
4928 from a form, and to make the responses appear on the screen.  The web
4929 prompt is publicly available at \url{http://bertus.mazzo.li}, a sample
4930 session is shown Figure \ref{fig:web-prompt-one}.
4931
4932 \begin{figure}[t]
4933   \includegraphics[width=\textwidth]{web-prompt.png}
4934   \caption{A sample run of the web prompt.}
4935   \label{fig:web-prompt-one}
4936 \end{figure}
4937
4938
4939
4940 \section{Evaluation}
4941 \label{sec:evaluation}
4942
4943 Going back to our goals in Section \ref{sec:contributions}, we feel that
4944 this thesis fills a gap in the description of observational type theory.
4945 In the design of \mykant\ we willingly patterned the core features
4946 against the ones present in Agda, with the hope that future implementors
4947 will be able to refer to this document without embarking on the same
4948 adventure themselves.  We gave an original account of heterogeneous
4949 equality by showing that in a cumulative hierarchy we can keep
4950 equalities as small as we would be able too with a separate notion of
4951 type equality.  As a side effect of developing \mykant, we also gave an
4952 original account of bidirectional type checking for user defined types,
4953 which get rid of many types while keeping the language very simple.
4954
4955 Through the design of the theory of \mykant\ we have followed an
4956 approach where study and implementation were continuously interleaved,
4957 as a `reality check' for the ideas that we wished to implement.  Given
4958 the great effort necessary to build a theorem prover capable of
4959 `real-world' proofs we have not attempted to compare \mykant's
4960 capabilities to those of Agda and Coq, the theorem provers that the
4961 author is most familiar with and in general two of the main players in
4962 the field.  However we have ported a lot of simpler examples to check
4963 that the key features are working, some of which have been used in the
4964 previous sections and are reproduced in the appendices\footnote{The full
4965 list is available in the repository:
4966 \url{https://github.com/bitonic/kant/tree/master/data/samples/good}.}.
4967 A full example of interaction with \mykant\ is given in Section
4968 \ref{sec:type-holes}.
4969
4970 The main culprits for the delays in the implementation are two issues
4971 that revealed themselves to be far less obvious than what the author
4972 predicted.  The first, as we have already remarked in Section
4973 \ref{sec:term-repr}, is to have an adequate term representation that
4974 lets us express the right constructs in a safe way.  There is still no
4975 widely accepted solution to this problem, which is approached in many
4976 different ways both in the literature and in the programming
4977 practice. The second aspect is the treatment of user defined data types.
4978 Again, the best techniques to implement them in a dependently typed
4979 setting still have not crystallised and implementors reinvent many
4980 wheels each time a new system is built.  The author is still conflicted
4981 on whether having user defined types at all it is the right decision:
4982 while they are essential, the recent discovery of a paper by
4983 \cite{dagand2012elaborating} describing a way to efficiently encode
4984 user-defined data types to a set of core primitives---an option that
4985 seems very attractive.
4986
4987 In general, implementing dependently typed languages is still a poorly
4988 understood practice, and almost every stage requires experimentation on
4989 behalf of the author.  Another example is the treatment of the implicit
4990 hierarchy, where no resources are present describing the problem from an
4991 implementation perspective (we described our approach in Section
4992 \ref{sec:hier-impl}).  Hopefully this state of things will change in the
4993 near future, and recent publications are promising in this direction,
4994 for example an unpublished paper by \cite{Brady2013} describing his
4995 implementation of the Idris programming language.  Our ultimate goal is
4996 to be a part of this collective effort.
4997
4998 \subsection{A type holes tutorial}
4999 \label{sec:type-holes}
5000
5001 As a taster and showcase for the capabilities of \mykant, we present an
5002 interactive session with the \mykant\ REPL.  While doing so, we present
5003 a feature that we still have not covered: type holes.
5004
5005 Type holes are, in the author's opinion, one of the `killer' features of
5006 interactive theorem provers, and one that is begging to be exported to
5007 mainstream programming---although it is much more effective in a
5008 well-typed, functional setting.  The idea is that when we are developing
5009 a proof or a program we can insert a hole to have the software tell us
5010 the type expected at that point.  Furthermore, we can ask for the type
5011 of variables in context, to better understand our surroundings.
5012
5013 In \mykant\ we use type holes by putting them where a term should go.
5014 We need to specify a name for the hole and then we can put as many terms
5015 as we like in it.  \mykant\ will tell us which type it is expecting for
5016 the term where the hole is, and the type for each  term that we have
5017 included.  For example if we had:
5018 \begin{Verbatim}
5019 plus [m n : Nat] : Nat ⇒ (
5020     {| h1 m n |}
5021 )
5022 \end{Verbatim}
5023 And we loaded the file in \mykant, we would get:
5024 \begin{Verbatim}[frame=leftline]
5025 >>> :l plus.ka
5026 Holes:
5027   h1 : Nat
5028     m : Nat
5029     n : Nat
5030 \end{Verbatim}
5031
5032 Suppose we wanted to define the `less or equal' ordering on natural
5033 numbers as described in Section \ref{sec:user-type}.  We will
5034 incrementally build our functions in a file called \texttt{le.ka}.
5035 First we define the necessary types, all of which we know well by now:
5036 \begin{Verbatim}
5037 data Nat : ⋆ ⇒ { zero : Nat | suc : Nat → Nat }
5038
5039 data Empty : ⋆ ⇒ { }
5040 absurd [A : ⋆] [p : Empty] : A ⇒ (
5041     Empty-Elim p (λ _ ⇒ A)
5042 )
5043
5044 record Unit : ⋆ ⇒ tt { }
5045 \end{Verbatim}
5046 Then fire up \mykant, and load the file:
5047 \begin{Verbatim}[frame=leftline]
5048 % ./bertus
5049 B E R T U S
5050 Version 0.0, made in London, year 2013.
5051 >>> :l le.ka
5052 OK
5053 \end{Verbatim}
5054 So far so good.  Our definition will be defined by recursion on a
5055 natural number \texttt{n}, which will return a function that given
5056 another number \texttt{m} will return the trivial type \texttt{Unit} if
5057 $\texttt{n} \le \texttt{m}$, and the \texttt{Empty} type otherwise.
5058 However we are still not sure on how to define it, so we invoke
5059 $\texttt{Nat-Elim}$, the eliminator for natural numbers, and place holes
5060 instead of arguments.  In the file we will write:
5061 \begin{Verbatim}
5062 le [n : Nat] : Nat → ⋆ ⇒ (
5063   Nat-Elim n (λ _ ⇒ Nat → ⋆)
5064     {|h1|}
5065     {|h2|}
5066 )
5067 \end{Verbatim}
5068 And then we reload in \mykant:
5069 \begin{Verbatim}[frame=leftline]
5070 >>> :r le.ka
5071 Holes:
5072   h1 : Nat → ⋆
5073   h2 : Nat → (Nat → ⋆) → Nat → ⋆
5074 \end{Verbatim}
5075 Which tells us what types we need to satisfy in each hole.  However, it
5076 is not that clear what does what in each hole, and thus it is useful to
5077 have a definition vacuous in its arguments just to clear things up.  We
5078 will use \texttt{Le} aid in reading the goal, with \texttt{Le m n} as a
5079 reminder that we to return the type corresponding to $\texttt{m} ≤
5080 \texttt{n}$:
5081 \begin{Verbatim}
5082 Le [m n : Nat] : ⋆ ⇒ ⋆
5083
5084 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5085   Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5086     {|h1|}
5087     {|h2|}
5088 )
5089 \end{Verbatim}
5090 \begin{Verbatim}[frame=leftline]
5091 >>> :r le.ka
5092 Holes:
5093   h1 : [m : Nat] → Le zero m
5094   h2 : [x : Nat] → ([m : Nat] → Le x m) → [m : Nat] → Le (suc x) m
5095 \end{Verbatim}
5096 This is much better!  \mykant, when printing terms, does not substitute
5097 top-level names for their bodies, since usually the resulting term is
5098 much clearer.  As a nice side-effect, we can use tricks like this to
5099 find guidance.
5100
5101 In this case in the first case we need to return, given any number
5102 \texttt{m}, $0 \le \texttt{m}$.  The trivial type will do, since every
5103 number is less or equal than zero:
5104 \begin{Verbatim}
5105 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5106   Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5107     (λ _ ⇒ Unit)
5108     {|h2|}
5109 )
5110 \end{Verbatim}
5111 \begin{Verbatim}[frame=leftline]
5112 >>> :r le.ka
5113 Holes:
5114   h2 : [x : Nat] → ([m : Nat] → Le x m) → [m : Nat] → Le (suc x) m
5115 \end{Verbatim}
5116 Now for the important case.  We are given our comparison function for a
5117 number, and we need to produce the function for the successor.  Thus, we
5118 need to re-apply the induction principle on the other number, \texttt{m}:
5119 \begin{Verbatim}
5120 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5121   Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5122     (λ _ ⇒ Unit)
5123     (λ n' f m ⇒ Nat-Elim m (λ m' ⇒ Le (suc n') m') {|h2|} {|h3|})
5124 )
5125 \end{Verbatim}
5126 \begin{Verbatim}[frame=leftline]
5127 >>> :r le.ka
5128 Holes:
5129   h2 : ⋆
5130   h3 : [x : Nat] → Le (suc n') x → Le (suc n') (suc x)
5131 \end{Verbatim}
5132 In the first hole we know that the second number is zero, and thus we
5133 can return empty.  In the second case, we can use the recursive argument
5134 \texttt{f} on the two numbers:
5135 \begin{Verbatim}
5136 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5137   Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5138     (λ _ ⇒ Unit)
5139     (λ n' f m ⇒
5140        Nat-Elim m (λ m' ⇒ Le (suc n') m') Empty (λ f _ ⇒ f m'))
5141 )
5142 \end{Verbatim}
5143 We can verify that our function works as expected:
5144 \begin{Verbatim}[frame=leftline]
5145 >>> :e le zero zero
5146 Unit
5147 >>> :e le zero (suc zero)
5148 Unit
5149 >>> :e le (suc (suc zero)) (suc zero)
5150 Empty
5151 \end{Verbatim}
5152 The other functionality of type holes is examining types of things in
5153 context.  Going back to the examples in Section \ref{sec:term-types}, we can
5154 implement the safe \texttt{head} function with our newly defined
5155 \texttt{le}:
5156 \begin{Verbatim}
5157 data List : [A : ⋆] → ⋆ ⇒
5158   { nil : List A | cons : A → List A → List A }
5159
5160 length [A : ⋆] [l : List A] : Nat ⇒ (
5161   List-Elim l (λ _ ⇒ Nat) zero (λ _ _ n ⇒ suc n)
5162 )
5163
5164 gt [n m : Nat] : ⋆ ⇒ (le (suc m) n)
5165
5166 head [A : ⋆] [l : List A] : gt (length A l) zero → A ⇒ (
5167   List-Elim l (λ l ⇒ gt (length A l) zero → A)
5168     (λ p ⇒ {|h1 p|})
5169     {|h2|}
5170 )
5171 \end{Verbatim}
5172 We define \texttt{List}s, a polymorphic \texttt{length} function, and
5173 express $<$ (\texttt{gt}) in terms of $\le$.  Then, we set up the type
5174 for our \texttt{head} function.  Given a list and a proof that its
5175 length is greater than zero, we return the first element.  If we load
5176 this in \mykant, we get:
5177 \begin{Verbatim}[frame=leftline]
5178 >>> :r le.ka
5179 Holes:
5180   h1 : A
5181     p : Empty
5182   h2 : [x : A] [x1 : List A] →
5183        (gt (length A x1) zero → A) →
5184        gt (length A (cons x x1)) zero → A
5185 \end{Verbatim}
5186 In the first case (the one for \texttt{nil}), we have a proof of
5187 \texttt{Empty}---surely we can use \texttt{absurd} to get rid of that
5188 case.  In the second case we simply return the element in the
5189 \texttt{cons}:
5190 \begin{Verbatim}
5191 head [A : ⋆] [l : List A] : gt (length A l) zero → A ⇒ (
5192   List-Elim l (λ l ⇒ gt (length A l) zero → A)
5193     (λ p ⇒ absurd A p)
5194     (λ x _ _ _ ⇒ x)
5195 )
5196 \end{Verbatim}
5197 Now, if we tried to get the head of an empty list, we face a problem:
5198 \begin{Verbatim}[frame=leftline]
5199 >>> :t head Nat nil
5200 Type: Empty → Nat
5201 \end{Verbatim}
5202 We would have to provide something of type \texttt{Empty}, which
5203 hopefully should be impossible.  For non-empty lists, on the other hand,
5204 things run smoothly:
5205 \begin{Verbatim}[frame=leftline]
5206 >>> :t head Nat (cons zero nil)
5207 Type: Unit → Nat
5208 >>> :e head Nat (cons zero nil) tt
5209 zero
5210 \end{Verbatim}
5211 This should give a vague idea of why type holes are so useful and in
5212 more in general about the development process in \mykant.  Most
5213 interactive theorem provers offer some kind of facility
5214 to... interactively develop proofs, usually much more powerful than the
5215 fairly bare tools present in \mykant.  Agda in particular offers a
5216 celebrated interactive mode for the \texttt{Emacs} text editor.
5217
5218 \section{Future work}
5219 \label{sec:future-work}
5220
5221 The first move that the author plans to make is to work towards a simple
5222 but powerful term representation.  A good plan seems to be to associate
5223 each type (terms, telescopes, etc.) with what we can substitute
5224 variables with, so that the term type will be associated with itself,
5225 while telescopes and propositions will be associated to terms.  This can
5226 probably be accomplished elegantly with Haskell's \emph{type families}
5227 \citep{chakravarty2005associated}.  After achieving a more solid
5228 machinery for terms, implementing observational equality fully should
5229 prove relatively easy.
5230
5231 Beyond this steps, we can go in many directions to improve the
5232 system that we described---here we review the main ones.
5233
5234 \begin{description}
5235 \item[Pattern matching and recursion] Eliminators are very clumsy,
5236   and using them can be especially frustrating if we are used to writing
5237   functions via explicit recursion.  \cite{Gimenez1995} showed how to
5238   reduce well-founded recursive definitions to primitive recursors.
5239   Intuitively, defining a function through an eliminators corresponds to
5240   pattern matching and recursively calling the function on the recursive
5241   occurrences of the type we matched against.
5242
5243   Nested pattern matching can be justified by identifying a notion of
5244   `structurally smaller', and allowing recursive calls on all smaller
5245   arguments.  Epigram goes all the way and actually implements recursion
5246   exclusively by providing a convenient interface to the two constructs
5247   above \citep{EpigramTut, McBride2004}.
5248
5249   However as we extend the flexibility in our recursion elaborating
5250   definitions to eliminators becomes more and more laborious.  For
5251   example we might want mutually recursive definitions and definitions
5252   that terminate relying on the structure of two arguments instead of
5253   just one.  For this reason both Agda and Coq (Agda putting more
5254   effort) let the user write recursive definitions freely, and then
5255   employ an external syntactic one the recursive calls to ensure that
5256   the definitions are terminating.
5257
5258   Moreover, if we want to use dependently typed languages for
5259   programming purposes, we will probably want to sidestep the
5260   termination checker and write a possibly non-terminating function;
5261   maybe because proving termination is particularly difficult.  With
5262   explicit recursion this amounts to turning off a check, if we have
5263   only eliminators it is impossible.
5264
5265 \item[More powerful data types] A popular improvement on basic data
5266   types are inductive families \citep{Dybjer1991}, where the parameters
5267   for the type constructors can change based on the data constructors,
5268   which lets us express naturally types such as $\mytyc{Vec} : \mynat
5269   \myarr \mytyp$, which given a number returns the type of lists of that
5270   length, or $\mytyc{Fin} : \mynat \myarr \mytyp$, which given a number
5271   $n$ gives the type of numbers less than $n$.  This apparent omission
5272   was motivated by the fact that inductive families can be represented
5273   by adding equalities concerning the parameters of the type
5274   constructors as arguments to the data constructor, in much the same
5275   way that Generalised Abstract Data Types \citep{GHC} are handled in
5276   Haskell.  Interestingly the modified version of System F that lies at
5277   the core of recent versions of GHC features coercions reminiscent of
5278   those found in OTT, motivated precisely by the need to implement GADTs
5279   in an elegant way \citep{Sulzmann2007}.
5280
5281   Another concept introduced by \cite{dybjer2000general} is
5282   induction-recursion, where we define a data type in tandem with a
5283   function on that type.  This technique has proven extremely useful to
5284   define embeddings of other calculi in an host language, by defining
5285   the representation of the embedded language as a data type and at the
5286   same time a function decoding from the representation to a type in the
5287   host language.  The decoding function is then used to define the data
5288   type for the embedding itself, for example by reusing the host's
5289   language functions to describe functions in the embedded language,
5290   with decoded types as arguments.
5291
5292   It is also worth mentioning that in recent times there has been work
5293   \citep{dagand2012elaborating, chapman2010gentle} to show how to define
5294   a set of primitives that data types can be elaborated into.  The big
5295   advantage of the approach proposed is enabling a very powerful notion
5296   of generic programming, by writing functions working on the
5297   `primitive' types as to be workable by all the other `compatible'
5298   elaborated user defined types.  This has been a considerable problem
5299   in the dependently type world, where we often define types which are
5300   more `strongly typed' version of similar structures,\footnote{For
5301     example the $\mytyc{OList}$ presented in Section \ref{sec:user-type}
5302     being a `more typed' version of an ordinary list.} and then find
5303   ourselves forced to redefine identical operations on both types.
5304
5305 \item[Pattern matching and inductive families] The notion of inductive
5306   family also yields a more interesting notion of pattern matching,
5307   since matching on an argument influences the value of the parameters
5308   of the type of said argument.  This means that pattern matching
5309   influences the context, which can be exploited to constraint the
5310   possible data constructors for \emph{other} arguments
5311   \citep{McBride2004}.
5312
5313 \item[Type inference] While bidirectional type checking helps at a very
5314   low cost of implementation and complexity, a much more powerful weapon
5315   is found in \emph{pattern unification}, which allows Hindley-Milner
5316   style inference for dependently typed languages.  Unification for
5317   higher order terms is undecidable and unification problems do not
5318   always have a most general unifier \citep{huet1973undecidability}.
5319   However \cite{miller1992unification} identified a decidable fragment
5320   of higher order unification commonly known as pattern unification,
5321   which is employed in most theorem provers to drastically reduce the
5322   number of type annotations.  \cite{gundrytutorial} provide a tutorial
5323   on this practice.
5324
5325 \item[Coinductive data types] When we specify inductive data types, we
5326   do it by specifying its \emph{constructors}---functions with the type
5327   we are defining as codomain.  Then, we are offered way of compute by
5328   recursively \emph{destructing} or \emph{eliminating} a member of the
5329   defined data type.
5330
5331   Coinductive data types are the dual of this approach.  We specify ways
5332   to destruct data, and we are given a way to generate the defined type
5333   by repeatedly `unfolding' starting from some seed data.  For example,
5334   we could defined infinite streams by specifying a $\myfun{head}$ and
5335   $\myfun{tail}$ destructors---here using a syntax reminiscent of
5336   \mykant\ records:
5337   \[
5338   \begin{array}{@{}l}
5339     \mysyn{codata}\ \mytyc{Stream}\myappsp (\myb{A} {:} \mytyp)\ \mysyn{where} \\
5340     \myind{2} \{ \myfun{head} : \myb{A}, \myfun{tail} : \mytyc{Stream} \myappsp \myb{A}\}
5341   \end{array}
5342   \]
5343   which will hopefully give us something like
5344   \[
5345   \begin{array}{@{}l}
5346     \myfun{head} : (\myb{A}{:}\mytyp) \myarr \mytyc{Stream} \myappsp \myb{A} \myarr \myb{A} \\
5347     \myfun{tail} : (\myb{A}{:}\mytyp) \myarr \mytyc{Stream} \myappsp \myb{A} \myarr \mytyc{Stream} \myappsp \myb{A} \\
5348     \mytyc{Stream}.\mydc{unfold} : (\myb{A}\, \myb{B} {:} \mytyp) \myarr (\myb{A} \myarr \myb{B} \myprod \myb{A}) \myarr \myb{A} \myarr \mytyc{Stream} \myappsp \myb{B}
5349   \end{array}
5350   \]
5351   Where, in $\mydc{unfold}$, $\myb{B} \myprod \myb{A}$ represents the
5352   fields of $\mytyc{Stream}$ but with the recursive occurrence replaced
5353   by the `seed' type $\myb{A}$.
5354
5355   Beyond simple infinite types like $\mytyc{Stream}$, coinduction is
5356   particularly useful to write non-terminating programs like servers or
5357   software interacting with a user, while guaranteeing their liveliness.
5358   Moreover it lets us model possibly non-terminating computations in an
5359   elegant way \citep{Capretta2005}, enabling for example the study of
5360   operational semantics for non-terminating languages
5361   \citep{Danielsson2012}.
5362  
5363   \cite{cockett1992charity} pioneered this approach in their programming
5364   language Charity, and coinduction has since been adopted in systems
5365   such as Coq \citep{Gimenez1996} and Agda.  However these
5366   implementations are unsatisfactory, since Coq's break subject
5367   reduction; and Agda, to avoid this problem, does not allow types to
5368   depend on the unfolding of codata.  \cite{mcbride2009let} has shown
5369   how observational equality can help to resolve these issues, since we
5370   can reason about the unfoldings in a better way, like we reason about
5371   functions' extensional behaviour.
5372 \end{description}
5373
5374 The author looks forward to the study and possibly the implementation of
5375 these ideas in the years to come.
5376
5377 \newpage{}
5378
5379 \appendix
5380
5381 \section{Notation and syntax}
5382 \label{app:notation}
5383
5384 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
5385 the type of relation being established and the syntactic elements appearing,
5386 for example
5387
5388 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
5389   Typing derivations here.
5390 }
5391
5392 In the languages presented and Agda code samples we also highlight the syntax,
5393 following a uniform colour, capitalisation, and font style convention:
5394
5395 \begin{center}
5396   \begin{tabular}{c | l}
5397     $\mytyc{Sans}$   & Type constructors. \\
5398     $\mydc{sans}$    & Data constructors. \\
5399     % $\myfld{sans}$  & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
5400     $\mysyn{roman}$  & Keywords of the language. \\
5401     $\myfun{roman}$  & Defined values and destructors. \\
5402     $\myb{math}$     & Bound variables.
5403   \end{tabular}
5404 \end{center}
5405
5406 When presenting grammars, we use a word in $\mysynel{math}$ font
5407 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate
5408 nonterminals. Additionally, we use quite flexibly a $\mysynel{math}$
5409 font to indicate a syntactic element in derivations or meta-operations.
5410 More specifically, terms are usually indicated by lowercase letters
5411 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase
5412 letter (often $\mytya$, $\mytyb$, or $\mytycc$).
5413
5414 When presenting type derivations, we often abbreviate and present multiple
5415 conclusions, each on a separate line:
5416 \begin{prooftree}
5417   \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
5418   \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
5419   \noLine
5420   \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
5421 \end{prooftree}
5422 We often present `definitions' in the described calculi and in
5423 $\mykant$\ itself, like so:
5424 \[
5425 \begin{array}{@{}l}
5426   \myfun{name} : \mytysyn \\
5427   \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
5428 \end{array}
5429 \]
5430 To define operators, we use a mixfix notation similar
5431 to Agda, where $\myarg$s denote arguments:
5432 \[
5433 \begin{array}{@{}l}
5434   \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
5435   \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
5436 \end{array}
5437 \]
5438 In explicitly typed systems, we omit type annotations when they
5439 are obvious, e.g. by not annotating the type of parameters of
5440 abstractions or of dependent pairs.\\
5441 We introduce multiple arguments in one go in arrow types:
5442 \[
5443   (\myb{x}\, \myb{y} {:} \mytya) \myarr \cdots = (\myb{x} {:} \mytya) \myarr (\myb{y} {:} \mytya) \myarr \cdots
5444 \]
5445 and in abstractions:
5446 \[
5447 \myabs{\myb{x}\myappsp\myb{y}}{\cdots} = \myabs{\myb{x}}{\myabs{\myb{y}}{\cdots}}
5448 \]
5449 We also omit arrows to abbreviate types:
5450 \[
5451 (\myb{x} {:} \mytya)(\myb{y} {:} \mytyb) \myarr \cdots =
5452 (\myb{x} {:} \mytya) \myarr (\myb{y} {:} \mytyb) \myarr \cdots
5453 \]
5454
5455 Meta operations names are displayed in $\mymeta{smallcaps}$ and
5456 written in a pattern matching style, also making use of boolean guards.
5457 For example, a meta operation operating on a context and terms might
5458 look like this:
5459 \[
5460 \begin{array}{@{}l}
5461   \mymeta{quux}(\myctx, \myb{x}) \mymetaguard \myb{x} \in \myctx \mymetagoes \myctx(\myb{x}) \\
5462   \mymeta{quux}(\myctx, \myb{x}) \mymetagoes \mymeta{outofbounds} \\
5463   \myind{2} \vdots
5464 \end{array}
5465 \]
5466
5467 From time to time we give examples in the Haskell programming
5468 language as defined by \cite{Haskell2010}, which we typeset in
5469 \texttt{teletype} font.  I assume that the reader is already familiar
5470 with Haskell, plenty of good introductions are available
5471 \citep{LYAH,ProgInHask}.
5472
5473 Examples of \mykant\ code will be typeset nicely with \LaTeX in Section
5474 \ref{sec:kant-theory}, to adjust with the rest of the presentation; and
5475 in \texttt{teletype} font in the rest of the document, including Section
5476 \ref{sec:kant-practice} and in the appendices.  All the \mykant\ code
5477 shown is meant to be working and ready to be inputted in a \mykant\
5478 prompt or loaded from a file. Snippets of sessions in the \mykant\
5479 prompt will be displayed with a left border, to distinguish them from
5480 snippets of code:
5481 \begin{Verbatim}[frame=leftline]
5482 >>> :t ⋆
5483 Type: ⋆
5484 \end{Verbatim}
5485
5486 \section{Code}
5487
5488 \subsection{ITT renditions}
5489 \label{app:itt-code}
5490
5491 \subsubsection{Agda}
5492 \label{app:agda-itt}
5493
5494 Note that in what follows rules for `base' types are
5495 universe-polymorphic, to reflect the exposition.  Derived definitions,
5496 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
5497 that in the theory presented we don't have universe polymorphism.
5498
5499 \begin{code}
5500 module ITT where
5501   open import Level
5502
5503   data Empty : Set where
5504
5505   absurd : ∀ {a} {A : Set a} → Empty → A
5506   absurd ()
5507
5508   ¬_ : ∀ {a} → (A : Set a) → Set a
5509   ¬ A = A → Empty
5510
5511   record Unit : Set where
5512     constructor tt
5513
5514   record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
5515     constructor _,_
5516     field
5517       fst  : A
5518       snd  : B fst
5519   open _×_ public
5520
5521   data Bool : Set where
5522     true false : Bool
5523
5524   if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
5525   if true / _ then x else _ = x
5526   if false / _ then _ else x = x
5527
5528   if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
5529   if_then_else_ x {P} = if_/_then_else_ x P
5530
5531   data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
5532     _◁_ : (s : S) → (P s → W S P) → W S P
5533
5534   rec : ∀ {a b} {S : Set a} {P : S → Set b}
5535     (C : W S P → Set) →       -- some conclusion we hope holds
5536     ((s : S) →                -- given a shape...
5537      (f : P s → W S P) →      -- ...and a bunch of kids...
5538      ((p : P s) → C (f p)) →  -- ...and C for each kid in the bunch...
5539      C (s ◁ f)) →             -- ...does C hold for the node?
5540     (x : W S P) →             -- If so, ...
5541     C x                       -- ...C always holds.
5542   rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
5543
5544 module Examples-→ where
5545   open ITT
5546
5547   data ℕ : Set where
5548     zero : ℕ
5549     suc : ℕ → ℕ
5550
5551   -- These pragmas are needed so we can use number literals.
5552   {-# BUILTIN NATURAL ℕ #-}
5553   {-# BUILTIN ZERO zero #-}
5554   {-# BUILTIN SUC suc #-}
5555
5556   data List (A : Set) : Set where
5557     [] : List A
5558     _∷_ : A → List A → List A
5559
5560   length : ∀ {A} → List A → ℕ
5561   length [] = zero
5562   length (_ ∷ l) = suc (length l)
5563
5564   _>_ : ℕ → ℕ → Set
5565   zero > _ = Empty
5566   suc _ > zero = Unit
5567   suc x > suc y = x > y
5568
5569   head : ∀ {A} → (l : List A) → length l > 0 → A
5570   head [] p = absurd p
5571   head (x ∷ _) _ = x
5572
5573 module Examples-× where
5574   open ITT
5575   open Examples-→
5576
5577   even : ℕ → Set
5578   even zero = Unit
5579   even (suc zero) = Empty
5580   even (suc (suc n)) = even n
5581
5582   6-even : even 6
5583   6-even = tt
5584
5585   5-not-even : ¬ (even 5)
5586   5-not-even = absurd
5587   
5588   there-is-an-even-number : ℕ × even
5589   there-is-an-even-number = 6 , 6-even
5590
5591   _∨_ : (A B : Set) → Set
5592   A ∨ B = Bool × (λ b → if b then A else B)
5593
5594   left : ∀ {A B} → A → A ∨ B
5595   left x = true , x
5596
5597   right : ∀ {A B} → B → A ∨ B
5598   right x = false , x
5599
5600   [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
5601   [ f , g ] x =
5602     (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
5603
5604 module Examples-W where
5605   open ITT
5606   open Examples-×
5607
5608   Tr : Bool → Set
5609   Tr b = if b then Unit else Empty
5610
5611   ℕ : Set
5612   ℕ = W Bool Tr
5613
5614   zero : ℕ
5615   zero = false ◁ absurd
5616
5617   suc : ℕ → ℕ
5618   suc n = true ◁ (λ _ → n)
5619
5620   plus : ℕ → ℕ → ℕ
5621   plus x y = rec
5622     (λ _ → ℕ)
5623     (λ b →
5624       if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
5625       then (λ _ f → (suc (f tt))) else (λ _ _ → y))
5626     x
5627
5628 module Equality where
5629   open ITT
5630   
5631   data _≡_ {a} {A : Set a} : A → A → Set a where
5632     refl : ∀ x → x ≡ x
5633
5634   ≡-elim : ∀ {a b} {A : Set a}
5635     (P : (x y : A) → x ≡ y → Set b) →
5636     ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
5637   ≡-elim P p (refl x) = p
5638
5639   subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
5640   subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
5641
5642   sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
5643   sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
5644
5645   trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
5646   trans x y z p q = subst (λ z′ → x ≡ z′) q p
5647
5648   cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y 
5649   cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
5650 \end{code}
5651
5652 \subsubsection{\mykant}
5653 \label{app:kant-itt}
5654
5655 The following things are missing: $\mytyc{W}$-types, since our
5656 positivity check is overly strict, and equality, since we haven't
5657 implemented that yet.
5658
5659 {\small
5660 \verbatiminput{itt.ka}
5661 }
5662
5663 \subsection{\mykant\ examples}
5664 \label{app:kant-examples}
5665
5666 {\small
5667 \verbatiminput{examples.ka}
5668 }
5669
5670 \subsection{\mykant' hierachy}
5671 \label{app:hurkens}
5672
5673 This rendition of the Hurken's paradox does not type check with the
5674 hierachy enabled, type checks and loops without it.  Adapted from an
5675 Agda version, available at
5676 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
5677
5678 {\small
5679 \verbatiminput{hurkens.ka}
5680 }
5681
5682 \subsection{Term representation}
5683 \label{app:termrep}
5684
5685 Data type for terms in \mykant.
5686
5687 {\small\begin{verbatim}-- A top-level name.
5688 type Id    = String
5689 -- A data/type constructor name.
5690 type ConId = String
5691
5692 -- A term, parametrised over the variable (`v') and over the reference
5693 -- type used in the type hierarchy (`r').
5694 data Tm r v
5695     = V v                        -- Variable.
5696     | Ty r                       -- Type, with a hierarchy reference.
5697     | Lam (TmScope r v)          -- Abstraction.
5698     | Arr (Tm r v) (TmScope r v) -- Dependent function.
5699     | App (Tm r v) (Tm r v)      -- Application.
5700     | Ann (Tm r v) (Tm r v)      -- Annotated term.
5701       -- Data constructor, the first ConId is the type constructor and
5702       -- the second is the data constructor.
5703     | Con ADTRec ConId ConId [Tm r v]
5704       -- Data destrutor, again first ConId being the type constructor
5705       -- and the second the name of the eliminator.
5706     | Destr ADTRec ConId Id (Tm r v)
5707       -- A type hole.
5708     | Hole HoleId [Tm r v]
5709       -- Decoding of propositions.
5710     | Dec (Tm r v)
5711
5712       -- Propositions.
5713     | Prop r -- The type of proofs, with hierarchy reference.
5714     | Top
5715     | Bot
5716     | And (Tm r v) (Tm r v)
5717     | Forall (Tm r v) (TmScope r v)
5718       -- Heterogeneous equality.
5719     | Eq (Tm r v) (Tm r v) (Tm r v) (Tm r v)
5720
5721 -- Either a data type, or a record.
5722 data ADTRec = ADT | Rec
5723
5724 -- Either a coercion, or coherence.
5725 data Coeh = Coe | Coh\end{verbatim}
5726 }
5727
5728 \subsection{Graph and constraints modules}
5729 \label{app:constraint}
5730
5731 The modules are respectively named \texttt{Data.LGraph} (short for
5732 `labelled graph'), and \texttt{Data.Constraint}.  The type class
5733 constraints on the type parameters are not shown for clarity, unless
5734 they are meaningful to the function.  In practice we use the
5735 \texttt{Hashable} type class on the vertex to implement the graph
5736 efficiently with hash maps.
5737
5738 \subsubsection{\texttt{Data.LGraph}}
5739
5740 {\small
5741 \verbatiminput{graph.hs}
5742 }
5743
5744 \subsubsection{\texttt{Data.Constraint}}
5745
5746 {\small
5747 \verbatiminput{constraint.hs}
5748 }
5749
5750 \newpage{}
5751
5752 \bibliographystyle{authordate1}
5753 \bibliography{thesis}
5754
5755 \end{document}