more report
[bitonic-mengthesis.git] / thesis.lagda
1 \documentclass[report]{article}
2 \usepackage{etex}
3
4 %% Narrow margins
5 % \usepackage{fullpage}
6
7 %% Bibtex
8 \usepackage{natbib}
9
10 %% Links
11 \usepackage{hyperref}
12
13 %% Frames
14 \usepackage{framed}
15
16 %% Symbols
17 \usepackage[fleqn]{amsmath}
18 \usepackage{stmaryrd}           %llbracket
19
20 %% Proof trees
21 \usepackage{bussproofs}
22
23 %% Diagrams
24 \usepackage[all]{xy}
25
26 %% Quotations
27 \usepackage{epigraph}
28
29 %% Images
30 \usepackage{graphicx}
31
32 %% Subfigure
33 \usepackage{subcaption}
34
35 \usepackage{verbatim}
36
37 %% diagrams
38 \usepackage{tikz}
39 \usetikzlibrary{shapes,arrows,positioning}
40 % \usepackage{tikz-cd}
41 % \usepackage{pgfplots}
42
43
44 %% -----------------------------------------------------------------------------
45 %% Commands for Agda
46 \usepackage[english]{babel}
47 \usepackage[conor]{agda}
48 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
49 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
50 \renewcommand{\AgdaField}{\AgdaFunction}
51 % \definecolor{AgdaBound} {HTML}{000000}
52 \definecolor{AgdaHole} {HTML} {FFFF33}
53
54 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
55 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
56 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
57 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
58 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
59 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
60
61 \renewenvironment{code}%
62 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed\small}%
63 {\endpboxed\par\noindent%
64 \ignorespacesafterend\small}
65
66
67 %% -----------------------------------------------------------------------------
68 %% Commands
69
70 \newcommand{\mysyn}{\AgdaKeyword}
71 \newcommand{\mytyc}{\AgdaDatatype}
72 \newcommand{\mydc}{\AgdaInductiveConstructor}
73 \newcommand{\myfld}{\AgdaField}
74 \newcommand{\myfun}{\AgdaFunction}
75 \newcommand{\myb}[1]{\AgdaBound{$#1$}}
76 \newcommand{\myfield}{\AgdaField}
77 \newcommand{\myind}{\AgdaIndent}
78 \newcommand{\mykant}{\textsc{Kant}}
79 \newcommand{\mysynel}[1]{#1}
80 \newcommand{\myse}{\mysynel}
81 \newcommand{\mytmsyn}{\mysynel{term}}
82 \newcommand{\mysp}{\ }
83 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
84 \newcommand{\myappsp}{\hspace{0.07cm}}
85 \newcommand{\myapp}[2]{#1 \myappsp #2}
86 \newcommand{\mysynsep}{\ \ |\ \ }
87 \newcommand{\myITE}[3]{\myfun{If}\, #1\, \myfun{Then}\, #2\, \myfun{Else}\, #3}
88
89 \FrameSep0.2cm
90 \newcommand{\mydesc}[3]{
91   \noindent
92   \mbox{
93       \vspace{0.1cm}
94     \parbox{\textwidth}{
95       {\small
96         \hfill \textbf{#1} $#2$
97         \framebox[\textwidth]{
98           \parbox{\textwidth}{
99             \vspace{0.1cm}
100             \centering{
101               #3
102             }
103             \vspace{0.1cm}
104           }
105         }
106       }
107     }
108   }
109 }
110
111 \newcommand{\mytmt}{\mysynel{t}}
112 \newcommand{\mytmm}{\mysynel{m}}
113 \newcommand{\mytmn}{\mysynel{n}}
114 \newcommand{\myred}{\leadsto}
115 \newcommand{\mysub}[3]{#1[#2 / #3]}
116 \newcommand{\mytysyn}{\mysynel{type}}
117 \newcommand{\mybasetys}{K}
118 \newcommand{\mybasety}[1]{B_{#1}}
119 \newcommand{\mytya}{\myse{A}}
120 \newcommand{\mytyb}{\myse{B}}
121 \newcommand{\mytycc}{\myse{C}}
122 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
123 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
124 \newcommand{\myctx}{\Gamma}
125 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
126 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
127 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
128 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
129 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
130 \newcommand{\myunit}{\mytyc{Unit}}
131 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
132 \newcommand{\myfst}{\myfld{fst}}
133 \newcommand{\mysnd}{\myfld{snd}}
134 \newcommand{\myconst}{\myse{c}}
135 \newcommand{\myemptyctx}{\cdot}
136 \newcommand{\myhole}{\AgdaHole}
137 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
138 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
139 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
140 \newcommand{\myright}[1]{\mydc{right}_{#1}}
141 \newcommand{\myempty}{\mytyc{Empty}}
142 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
143 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
144 \newcommand{\myarg}{\_}
145 \newcommand{\myderivsp}{\vspace{0.3cm}}
146 \newcommand{\mytyp}{\mytyc{Type}}
147 \newcommand{\myneg}{\myfun{$\neg$}}
148 \newcommand{\myar}{\,}
149 \newcommand{\mybool}{\mytyc{Bool}}
150 \newcommand{\mytrue}{\mydc{true}}
151 \newcommand{\myfalse}{\mydc{false}}
152 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
153 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
154 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
155 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
156 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
157 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
158 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
159 \newcommand{\mylist}{\mytyc{List}}
160 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
161 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
162 \newcommand{\myfoldr}{\myfun{foldr}}
163 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
164 \newcommand{\mynodee}{\mathbin{\mydc{$\lhd$}}}
165 \newcommand{\mynode}[2]{\mynodee_{#1.#2}}
166 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
167 \newcommand{\mylub}{\sqcup}
168 \newcommand{\mydefeq}{\cong}
169 \newcommand{\myrefl}{\mydc{refl}}
170 \newcommand{\mypeq}[1]{\mathrel{\mytyc{=}_{#1}}}
171 \newcommand{\myjeqq}{\myfun{=-elim}}
172 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
173 \newcommand{\mysubst}{\myfun{subst}}
174 \newcommand{\myprsyn}{\myse{prop}}
175 \newcommand{\myprdec}[1]{\mathopen{\mytyc{$\llbracket$}} #1 \mathopen{\mytyc{$\rrbracket$}}}
176 \newcommand{\myand}{\mathrel{\mytyc{$\wedge$}}}
177 \newcommand{\myprfora}[3]{\forall #1 {:} #2. #3}
178 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
179 \newcommand{\mybot}{\mytyc{$\bot$}}
180 \newcommand{\mytop}{\mytyc{$\top$}}
181 \newcommand{\mycoe}{\myfun{coe}}
182 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
183 \newcommand{\mycoh}{\myfun{coh}}
184 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
185 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
186 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
187 \newcommand{\myprop}{\mytyc{Prop}}
188 \newcommand{\mytmup}{\mytmsyn\uparrow}
189 \newcommand{\mydefs}{\Delta}
190 \newcommand{\mynf}{\Downarrow}
191 \newcommand{\myinff}[3]{#1 \vdash #2 \Rightarrow #3}
192 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
193 \newcommand{\mychkk}[3]{#1 \vdash #2 \Leftarrow #3}
194 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
195 \newcommand{\myann}[2]{#1 : #2}
196 \newcommand{\mydeclsyn}{\myse{decl}}
197 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
198 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
199 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
200 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ \{ #4 \}}
201 \newcommand{\myelabt}{\vdash}
202 \newcommand{\myelabf}{\rhd}
203 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
204 \newcommand{\mytele}{\Delta}
205 \newcommand{\mytelee}{\delta}
206 \newcommand{\mydcctx}{\Gamma}
207 \newcommand{\mynamesyn}{\myse{name}}
208 \newcommand{\myvec}{\overrightarrow}
209 \newcommand{\mymeta}{\textsc}
210 \newcommand{\myhyps}{\mymeta{hyps}}
211 \newcommand{\mycc}{;}
212 \newcommand{\myemptytele}{\cdot}
213 \newcommand{\mymetagoes}{\Longrightarrow}
214 % \newcommand{\mytesctx}{\
215 \newcommand{\mytelesyn}{\myse{telescope}}
216 \newcommand{\myrecs}{\mymeta{recs}}
217 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
218 \newcommand{\mylet}{\mysyn{let}}
219 \newcommand{\myhead}{\mymeta{head}}
220 \newcommand{\mytake}{\mymeta{take}}
221 \newcommand{\myix}{\mymeta{ix}}
222 \newcommand{\myapply}{\mymeta{apply}}
223 \newcommand{\mydataty}{\mymeta{datatype}}
224 \newcommand{\myisreco}{\mymeta{record}}
225 \newcommand{\mydcsep}{\ |\ }
226 \newcommand{\mytree}{\mytyc{Tree}}
227 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
228 \newcommand{\mysigma}{\mytyc{$\Sigma$}}
229
230
231 %% -----------------------------------------------------------------------------
232
233 \title{\mykant: Implementing Observational Equality}
234 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
235 \date{June 2013}
236
237   \iffalse
238   \begin{code}
239     module thesis where
240   \end{code}
241   \fi
242
243 \begin{document}
244
245 \begin{titlepage}  
246   \centering
247
248   \maketitle
249   \thispagestyle{empty}
250
251   \begin{minipage}{0.4\textwidth}
252   \begin{flushleft} \large
253     \emph{Supervisor:}\\
254     Dr. Steffen \textsc{van Backel}
255   \end{flushleft}
256 \end{minipage}
257 \begin{minipage}{0.4\textwidth}
258   \begin{flushright} \large
259     \emph{Co-marker:} \\
260     Dr. Philippa \textsc{Gardner}
261   \end{flushright}
262 \end{minipage}
263
264 \end{titlepage}
265
266 \begin{abstract}
267   The marriage between programming and logic has been a very fertile one.  In
268   particular, since the simply typed lambda calculus (STLC), a number of type
269   systems have been devised with increasing expressive power.
270
271   Among this systems, Inutitionistic Type Theory (ITT) has been a very
272   popular framework for theorem provers and programming languages.
273   However, equality has always been a tricky business in ITT and related
274   theories.
275
276   In these thesis we will explain why this is the case, and present
277   Observational Type Theory (OTT), a solution to some of the problems
278   with equality.  We then describe $\mykant$, a theorem prover featuring
279   OTT in a setting more close to the one found in current systems.
280   Having implemented part of $\mykant$ as a Haskell program, we describe
281   some of the implementation issues faced.
282 \end{abstract}
283
284 \clearpage
285
286 \renewcommand{\abstractname}{Acknowledgements}
287 \begin{abstract}
288   I would like to thank Steffen van Backel, my supervisor, who was brave
289   enough to believe in my project and who provided much advice and
290   support.
291
292   I would also like to thank the Haskell and Agda community on
293   \texttt{IRC}, which guided me through the strange world of types; and
294   in particular Andrea Vezzosi and James Deikun, with whom I entertained
295   countless insightful discussions in the past year.  Andrea suggested
296   Observational Type Theory as a topic of study: this thesis would not
297   exist without him.  Before them, Tony Fields introduced me to Haskell,
298   unknowingly filling most of my free time from that time on.
299
300   Finally, much of the work stems from the research of Conor McBride,
301   who answered many of my doubts through these months.  I also owe him
302   the colours.
303 \end{abstract}
304
305 \clearpage
306
307 \tableofcontents
308
309 \clearpage
310
311 \section{Simple and not-so-simple types}
312 \label{sec:types}
313
314 \subsection{The untyped $\lambda$-calculus}
315
316 Along with Turing's machines, the earliest attempts to formalise computation
317 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
318 language encodes computation with a minimal syntax and no `data' in the
319 traditional sense, but just functions.  Here we give a brief overview of the
320 language, which will give the chance to introduce concepts central to the
321 analysis of all the following calculi.  The exposition follows the one found in
322 chapter 5 of \cite{Queinnec2003}.
323
324 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
325 and applications:
326
327 \mydesc{syntax}{ }{
328   $
329   \begin{array}{r@{\ }c@{\ }l}
330     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
331     x          & \in & \text{Some enumerable set of symbols}
332   \end{array}
333   $
334 }
335
336 Parenthesis will be omitted in the usual way:
337 $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
338 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
339
340 Abstractions roughly corresponds to functions, and their semantics is more
341 formally explained by the $\beta$-reduction rule:
342
343 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
344   $
345   \begin{array}{l}
346     \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{, where} \\
347     \myind{2}
348     \begin{array}{l@{\ }c@{\ }l}
349       \mysub{\myb{x}}{\myb{x}}{\mytmn} & = & \mytmn \\
350       \mysub{\myb{y}}{\myb{x}}{\mytmn} & = & y\text{, with } \myb{x} \neq y \\
351       \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & = & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
352       \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{x}}{\mytmm} \\
353       \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}}, \\
354       \multicolumn{3}{l}{\myind{2} \text{with $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
355     \end{array}
356   \end{array}
357   $
358 }
359
360 The care required during substituting variables for terms is required to avoid
361 name capturing.  We will use substitution in the future for other name-binding
362 constructs assuming similar precautions.
363
364 These few elements are of remarkable expressiveness, and in fact Turing
365 complete.  As a corollary, we must be able to devise a term that reduces forever
366 (`loops' in imperative terms):
367 {\small
368 \[
369   (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
370 \]
371 }
372
373 A \emph{redex} is a term that can be reduced.  In the untyped $\lambda$-calculus
374 this will be the case for an application in which the first term is an
375 abstraction, but in general we call aterm reducible if it appears to the left of
376 a reduction rule.  When a term contains no redexes it's said to be in
377 \emph{normal form}.  Given the observation above, not all terms reduce to a
378 normal forms: we call the ones that do \emph{normalising}, and the ones that
379 don't \emph{non-normalising}.
380
381 The reduction rule presented is not syntax directed, but \emph{evaluation
382   strategies} can be employed to reduce term systematically. Common evaluation
383 strategies include \emph{call by value} (or \emph{strict}), where arguments of
384 abstractions are reduced before being applied to the abstraction; and conversely
385 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
386 to proceed---in other words when we have an application where the function is
387 still not a $\lambda$. In both these reduction strategies we never reduce under
388 an abstraction: for this reason a weaker form of normalisation is used, where
389 both abstractions and normal forms are said to be in \emph{weak head normal
390   form}.
391
392 \subsection{The simply typed $\lambda$-calculus}
393
394 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
395 \emph{types} to them, and then check that the terms that we are forming make
396 sense given our typing rules \citep{Curry1934}.  The first most basic instance
397 of this idea takes the name of \emph{simply typed $\lambda$ calculus}, whose
398 rules are shown in figure \ref{fig:stlc}.
399
400 Our types contain a set of \emph{type variables} $\Phi$, which might
401 correspond to some `primitive' types; and $\myarr$, the type former for
402 `arrow' types, the types of functions.  The language is explicitly
403 typed: when we bring a variable into scope with an abstraction, we
404 declare its type.  Reduction is unchanged from the untyped
405 $\lambda$-calculus.
406
407 \begin{figure}[t]
408   \mydesc{syntax}{ }{
409     $
410     \begin{array}{r@{\ }c@{\ }l}
411       \mytmsyn   & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
412       (\myapp{\mytmsyn}{\mytmsyn}) \\
413       \mytysyn   & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn  \mysynsep \\
414       \myb{x}    & \in & \text{Some enumerable set of symbols} \\
415       \myse{\phi} & \in & \Phi
416     \end{array}
417     $
418   }
419   
420   \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
421       \begin{tabular}{ccc}
422         \AxiomC{$\myctx(x) = A$}
423         \UnaryInfC{$\myjud{\myb{x}}{A}$}
424         \DisplayProof
425         &
426         \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
427         \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
428         \DisplayProof
429         &
430         \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
431         \AxiomC{$\myjud{\mytmn}{\mytya}$}
432         \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
433         \DisplayProof
434       \end{tabular}
435 }
436   \caption{Syntax and typing rules for the STLC.  Reduction is unchanged from
437     the untyped $\lambda$-calculus.}
438   \label{fig:stlc}
439 \end{figure}
440
441 In the typing rules, a context $\myctx$ is used to store the types of bound
442 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
443 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
444
445 This typing system takes the name of `simply typed lambda calculus' (STLC), and
446 enjoys a number of properties.  Two of them are expected in most type systems
447 \citep{Pierce2002}:
448 \begin{description}
449 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
450   constructor does not appear on the left of the $\myred$ relation (currently
451   only $\lambda$), or it can take a step according to the evaluation rules.
452 \item[Preservation] If a well-typed term takes a step of evaluation, then the
453   resulting term is also well-typed, and preserves the previous type.  Also
454   known as \emph{subject reduction}.
455 \end{description}
456
457 However, STLC buys us much more: every well-typed term is normalising
458 \citep{Tait1967}.  It is easy to see that we can't fill the blanks if we want to
459 give types to the non-normalising term shown before:
460 \begin{equation*}
461   \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
462 \end{equation*}
463
464 This makes the STLC Turing incomplete.  We can recover the ability to loop by
465 adding a combinator that recurses:
466
467 \noindent
468 \begin{minipage}{0.5\textwidth}
469 \mydesc{syntax}{ } {
470   $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
471   \vspace{0.4cm}
472 }
473 \end{minipage} 
474 \begin{minipage}{0.5\textwidth}
475 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
476     \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
477     \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
478     \DisplayProof
479 }
480 \end{minipage} 
481
482 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
483     $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
484 }
485
486 This will deprive us of normalisation, which is a particularly bad thing if we
487 want to use the STLC as described in the next section.
488
489 \subsection{The Curry-Howard correspondence}
490
491 It turns out that the STLC can be seen a natural deduction system for
492 intuitionistic propositional logic.  Terms are proofs, and their types are the
493 propositions they prove.  This remarkable fact is known as the Curry-Howard
494 correspondence, or isomorphism.
495
496 The arrow ($\myarr$) type corresponds to implication.  If we wish to prove that
497 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
498 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
499 correct type:
500 {\small\[
501   \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
502 \]}
503 That is, function composition.  Going beyond arrow types, we can extend our bare
504 lambda calculus with useful types to represent other logical constructs, as
505 shown in figure \ref{fig:natded}.
506
507 \begin{figure}[t]
508 \mydesc{syntax}{ }{
509   $
510   \begin{array}{r@{\ }c@{\ }l}
511     \mytmsyn & ::= & \cdots \\
512              &  |  & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
513              &  |  & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
514                      \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
515                      \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
516              &  |  & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
517                      \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
518     \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
519   \end{array}
520   $
521 }
522
523 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
524     \begin{tabular}{cc}
525       $
526       \begin{array}{l@{ }l@{\ }c@{\ }l}
527         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
528           \myapp{\mytmm}{\mytmt} \\
529         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
530           \myapp{\mytmn}{\mytmt}
531       \end{array}
532       $
533       &
534       $
535       \begin{array}{l@{ }l@{\ }c@{\ }l}
536         \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
537         \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
538       \end{array}
539       $
540     \end{tabular}
541 }
542
543 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
544     \begin{tabular}{cc}
545       \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
546       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
547       \DisplayProof
548       &
549       \AxiomC{$\myjud{\mytmt}{\myempty}$}
550       \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
551       \DisplayProof
552     \end{tabular}
553
554   \myderivsp
555
556     \begin{tabular}{cc}
557       \AxiomC{$\myjud{\mytmt}{\mytya}$}
558       \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
559       \DisplayProof
560       &
561       \AxiomC{$\myjud{\mytmt}{\mytyb}$}
562       \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
563       \DisplayProof
564
565     \end{tabular}
566
567   \myderivsp
568
569     \begin{tabular}{cc}
570       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
571       \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
572       \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
573       \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
574       \DisplayProof
575     \end{tabular}
576
577   \myderivsp
578
579     \begin{tabular}{ccc}
580       \AxiomC{$\myjud{\mytmm}{\mytya}$}
581       \AxiomC{$\myjud{\mytmn}{\mytyb}$}
582       \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
583       \DisplayProof
584       &
585       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
586       \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
587       \DisplayProof
588       &
589       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
590       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
591       \DisplayProof
592     \end{tabular}
593 }
594 \caption{Rules for the extendend STLC.  Only the new features are shown, all the
595   rules and syntax for the STLC apply here too.}
596   \label{fig:natded}
597 \end{figure}
598
599 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
600 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
601 products---$\myprod$ here, tuples in Haskell) correspond to
602 conjunctions.  This is apparent looking at the ways to construct and
603 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
604 and $\myright{ }$ correspond to $\vee$ introduction, and
605 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
606 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
607 and $\mysnd$ to $\wedge$ elimination.
608
609 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
610 $\myempty$ corresponds to the logical $\bot$.  $\myunit$ has one introduction
611 rule ($\mytt$), and thus one inhabitant; and no eliminators.  $\myempty$ has no
612 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
613 }$), corresponding to the logical \emph{ex falso quodlibet}.
614
615 With these rules, our STLC now looks remarkably similar in power and use to the
616 natural deduction we already know.  $\myneg \mytya$ can be expressed as $\mytya
617 \myarr \myempty$.  However, there is an important omission: there is no term of
618 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
619 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
620 a type equivalent to those.
621
622 This has a considerable effect on our logic and it's no coincidence, since there
623 is no obvious computational behaviour for laws like the excluded middle.
624 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
625 and all the systems analysed will have this characteristic since they build on
626 the foundation of the STLC\footnote{There is research to give computational
627   behaviour to classical logic, but I will not touch those subjects.}.
628
629 As in logic, if we want to keep our system consistent, we must make sure that no
630 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
631 The variant of STLC presented here is indeed
632 consistent, a result that follows from the fact that it is
633 normalising. % TODO explain
634 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
635 desire for consistency.  The following term works for every type $\mytya$,
636 including bottom:
637 {\small\[
638 (\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya
639 \]}
640
641 \subsection{Inductive data}
642 \label{sec:ind-data}
643
644 To make the STLC more useful as a programming language or reasoning tool it is
645 common to include (or let the user define) inductive data types.  These comprise
646 of a type former, various constructors, and an eliminator (or destructor) that
647 serves as primitive recursor.
648
649 For example, we might add a $\mylist$ type constructor, along with an `empty
650 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor.  The eliminator for
651 lists will be the usual folding operation ($\myfoldr$).  See figure
652 \ref{fig:list}.
653
654 \begin{figure}[h]
655 \mydesc{syntax}{ }{
656   $
657   \begin{array}{r@{\ }c@{\ }l}
658     \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
659                      \mysynsep
660                      \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
661     \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
662   \end{array}
663   $
664 }
665 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
666   $
667   \begin{array}{l@{\ }c@{\ }l}
668     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
669
670     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
671     \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
672   \end{array}
673   $
674 }
675 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
676     \begin{tabular}{cc}
677       \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
678       \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
679       \DisplayProof
680       &
681       \AxiomC{$\myjud{\mytmm}{\mytya}$}
682       \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
683       \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
684       \DisplayProof
685     \end{tabular}
686   \myderivsp
687
688     \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
689     \AxiomC{$\myjud{\mytmm}{\mytyb}$}
690     \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
691     \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
692     \DisplayProof
693 }
694 \caption{Rules for lists in the STLC.}
695 \label{fig:list}
696 \end{figure}
697
698 In section \ref{sec:well-order} we will see how to give a general account of
699 inductive data.  %TODO does this make sense to have here?
700
701 \section{Intuitionistic Type Theory}
702 \label{sec:itt}
703
704 \subsection{Extending the STLC}
705
706 The STLC can be made more expressive in various ways.  \cite{Barendregt1991}
707 succinctly expressed geometrically how we can add expressivity:
708
709 $$
710 \xymatrix@!0@=1.5cm{
711   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
712   & & \lambda C \ar@{-}[dd]
713   \\
714   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
715   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
716   \\
717   & \lambda\underline\omega \ar@{-}'[r][rr]
718   & & \lambda P\underline\omega
719   \\
720   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
721   & & \lambda P \ar@{-}[ur]
722 }
723 $$
724 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
725 3 dimensions:
726 \begin{description}
727 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
728   types in our type signatures.  For example, we can define a polymorphic
729   identity function:
730   {\small\[\displaystyle
731   (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} : \mytyp) \myarr \myb{A} \myarr \myb{A}
732   \]}
733   The first and most famous instance of this idea has been System F.  This form
734   of polymorphism and has been wildly successful, also thanks to a well known
735   inference algorithm for a restricted version of System F known as
736   Hindley-Milner.  Languages like Haskell and SML are based on this discipline.
737 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
738   type operators.  For example we could define a function that given types $R$
739   and $\mytya$ forms the type that represents a value of type $\mytya$ in
740   continuation passing style: {\small\[\displaystyle(\myabss{\myb{A} \myar \myb{R}}{\mytyp}{(\myb{A}
741     \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp\]}
742 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
743   types', give great expressive power.  For example, we can have values of whose
744   type depend on a boolean:
745   {\small\[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
746   \myarr \mytyp\]}
747 \end{description}
748
749 All the systems preserve the properties that make the STLC well behaved.  The
750 system we are going to focus on, Intuitionistic Type Theory, has all of the
751 above additions, and thus would sit where $\lambda{C}$ sits in the
752 `$\lambda$-cube'.  It will serve as the logical `core' of all the other
753 extensions that we will present and ultimately our implementation of a similar
754 logic.
755
756 \subsection{A Bit of History}
757
758 Logic frameworks and programming languages based on type theory have a long
759 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
760 but then revised it since the original version was inconsistent due to its
761 impredicativity\footnote{In the early version there was only one universe
762   $\mytyp$ and $\mytyp : \mytyp$, see section \ref{sec:term-types} for an
763   explanation on why this causes problems.}.  For this reason he gave a revised
764 and consistent definition later \citep{Martin-Lof1984}.
765
766 A related development is the polymorphic $\lambda$-calculus, and specifically
767 the previously mentioned System F, which was developed independently by Girard
768 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
769 fact is that while System F is impredicative it is still consistent and strongly
770 normalising.  \cite{Coquand1986} further extended this line of work with the
771 Calculus of Constructions (CoC).
772
773 Most widely used interactive theorem provers are based on ITT.  Popular ones
774 include Agda \citep{Norell2007, Bove2009}, Coq \citep{Coq}, and Epigram
775 \citep{McBride2004, EpigramTut}.
776
777 \subsection{A note on inference}
778
779 % TODO do this, adding links to the sections about bidi type checking and
780 % implicit universes.
781 In the following text I will often omit explicit typing for abstractions or
782
783 Moreover, I will use $\mytyp$ without bothering to specify a
784 universe, with the silent assumption that the definition is consistent
785 regarding to the hierarchy.
786
787 \subsection{A simple type theory}
788 \label{sec:core-tt}
789
790 The calculus I present follows the exposition in \citep{Thompson1991},
791 and is quite close to the original formulation of predicative ITT as
792 found in \citep{Martin-Lof1984}.  The system's syntax and reduction
793 rules are presented in their entirety in figure \ref{fig:core-tt-syn}.
794 The typing rules are presented piece by piece.  Agda and \mykant\
795 renditions of the presented theory and all the examples is reproduced in
796 appendix \ref{app:itt-code}.
797
798 \begin{figure}[t]
799 \mydesc{syntax}{ }{
800   $
801   \begin{array}{r@{\ }c@{\ }l}
802     \mytmsyn & ::= & \myb{x} \mysynsep
803                      \mytyp_{l} \mysynsep
804                      \myunit \mysynsep \mytt \mysynsep
805                      \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
806              &  |  & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
807                      \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
808              &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
809                      \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
810                      (\myapp{\mytmsyn}{\mytmsyn}) \\
811              &  |  & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
812                      \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
813              &  |  & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
814              &  |  & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
815                      \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
816              &  |  & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
817     l        & \in & \mathbb{N}
818   \end{array}
819   $
820 }
821
822 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
823     \begin{tabular}{ccc}
824       $
825       \begin{array}{l@{ }l@{\ }c@{\ }l}
826         \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
827         \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
828       \end{array}
829       $
830       &
831       $
832       \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
833       $
834       &
835     $
836     \begin{array}{l@{ }l@{\ }c@{\ }l}
837       \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
838       \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
839     \end{array}
840     $
841     \end{tabular}
842
843     \myderivsp
844
845     $
846     \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
847     \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
848       \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
849     })}
850     $
851 }
852 \caption{Syntax and reduction rules for our type theory.}
853 \label{fig:core-tt-syn}
854 \end{figure}
855
856 \subsubsection{Types are terms, some terms are types}
857 \label{sec:term-types}
858
859 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
860     \begin{tabular}{cc}
861       \AxiomC{$\myjud{\mytmt}{\mytya}$}
862       \AxiomC{$\mytya \mydefeq \mytyb$}
863       \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
864       \DisplayProof
865       &
866       \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
867       \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
868       \DisplayProof
869     \end{tabular}
870 }
871
872 The first thing to notice is that a barrier between values and types that we had
873 in the STLC is gone: values can appear in types, and the two are treated
874 uniformly in the syntax.
875
876 While the usefulness of doing this will become clear soon, a consequence is
877 that since types can be the result of computation, deciding type equality is
878 not immediate as in the STLC.  For this reason we define \emph{definitional
879   equality}, $\mydefeq$, as the congruence relation extending
880 $\myred$---moreover, when comparing types syntactically we do it up to
881 renaming of bound names ($\alpha$-renaming).  For example under this
882 discipline we will find that
883 {\small\[
884 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}}
885 \]}
886 Types that are definitionally equal can be used interchangeably.  Here
887 the `conversion' rule is not syntax directed, but it is possible to
888 employ $\myred$ to decide term equality in a systematic way, by always
889 reducing terms to their normal forms before comparing them, so that a
890 separate conversion rule is not needed.  % TODO add section
891 Another thing to notice is that considering the need to reduce terms to
892 decide equality, it is essential for a dependently type system to be
893 terminating and confluent for type checking to be decidable.
894
895 Moreover, we specify a \emph{type hierarchy} to talk about `large'
896 types: $\mytyp_0$ will be the type of types inhabited by data:
897 $\mybool$, $\mynat$, $\mylist$, etc.  $\mytyp_1$ will be the type of
898 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
899 \mytyp_0 : \mytyp_1 : \cdots$.  Each type `level' is often called a
900 universe in the literature.  While it is possible to simplify things by
901 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
902 inconsistent for much the same reason that impredicative na\"{\i}ve set
903 theory is \citep{Hurkens1995}.  However various techniques can be
904 employed to lift the burden of explicitly handling universes, as we will
905 see in section \ref{sec:term-hierarchy}.
906
907 \subsubsection{Contexts}
908
909 \begin{minipage}{0.5\textwidth}
910   \mydesc{context validity:}{\myvalid{\myctx}}{
911       \begin{tabular}{cc}
912         \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
913         \UnaryInfC{$\myvalid{\myemptyctx}$}
914         \DisplayProof
915         &
916         \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
917         \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
918         \DisplayProof
919       \end{tabular}
920   }
921 \end{minipage} 
922 \begin{minipage}{0.5\textwidth}
923   \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
924       \AxiomC{$\myctx(x) = \mytya$}
925       \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
926       \DisplayProof
927   }
928 \end{minipage}
929 \vspace{0.1cm}
930
931 We need to refine the notion context to make sure that every variable appearing
932 is typed correctly, or that in other words each type appearing in the context is
933 indeed a type and not a value.  In every other rule, if no premises are present,
934 we assume the context in the conclusion to be valid.
935
936 Then we can re-introduce the old rule to get the type of a variable for a
937 context.
938
939 \subsubsection{$\myunit$, $\myempty$}
940
941 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
942     \begin{tabular}{ccc}
943       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
944       \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
945       \noLine
946       \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
947       \DisplayProof
948       &
949       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
950       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
951       \noLine
952       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
953       \DisplayProof
954       &
955       \AxiomC{$\myjud{\mytmt}{\myempty}$}
956       \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
957       \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
958       \noLine
959       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
960       \DisplayProof
961     \end{tabular}
962 }
963
964 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
965 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
966 sure that we are invoking $\myabsurd{}$ over a type.
967
968 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
969
970 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
971    \begin{tabular}{ccc}
972      \AxiomC{}
973      \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
974      \DisplayProof
975      &
976      \AxiomC{}
977      \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
978      \DisplayProof
979      &
980      \AxiomC{}
981       \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
982       \DisplayProof
983     \end{tabular}
984     \myderivsp
985
986     \AxiomC{$\myjud{\mytmt}{\mybool}$}
987     \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
988     \noLine
989     \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
990     \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
991     \DisplayProof
992 }
993
994 With booleans we get the first taste of the `dependent' in `dependent
995 types'.  While the two introduction rules ($\mytrue$ and $\myfalse$) are
996 not surprising, the typing rules for $\myfun{if}$ are.  In most strongly
997 typed languages we expect the branches of an $\myfun{if}$ statements to
998 be of the same type, to preserve subject reduction, since execution
999 could take both paths.  This is a pity, since the type system does not
1000 reflect the fact that in each branch we gain knowledge on the term we
1001 are branching on.  Which means that programs along the lines of
1002 {\small\[\text{\texttt{if null xs then head xs else 0}}\]}
1003 are a necessary, well typed, danger.
1004
1005 However, in a more expressive system, we can do better: the branches' type can
1006 depend on the value of the scrutinised boolean.  This is what the typing rule
1007 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1008 representing the scrutinised boolean type, and the branches are typechecked with
1009 the updated knowledge on the value of $\myb{x}$.
1010
1011 \subsubsection{$\myarr$, or dependent function}
1012
1013  \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1014      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1015      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1016      \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1017      \DisplayProof
1018
1019      \myderivsp
1020
1021     \begin{tabular}{cc}
1022       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1023       \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1024       \DisplayProof
1025       &
1026       \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1027       \AxiomC{$\myjud{\mytmn}{\mytya}$}
1028       \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1029       \DisplayProof
1030     \end{tabular}
1031 }
1032
1033 Dependent functions are one of the two key features that perhaps most
1034 characterise dependent types---the other being dependent products.  With
1035 dependent functions, the result type can depend on the value of the
1036 argument.  This feature, together with the fact that the result type
1037 might be a type itself, brings a lot of interesting possibilities.
1038 Following this intuition, in the introduction rule, the return type is
1039 typechecked in a context with an abstracted variable of lhs' type, and
1040 in the elimination rule the actual argument is substituted in the return
1041 type.  Keeping the correspondence with logic alive, dependent functions
1042 are much like universal quantifiers ($\forall$) in logic.
1043
1044 For example, assuming that we have lists and natural numbers in our
1045 language, using dependent functions we would be able to
1046 write:
1047 {\small\[
1048 \begin{array}{l}
1049 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1050 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1051 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1052                \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{>}} 0 \myarr
1053                \myb{A}
1054 \end{array}
1055 \]}
1056
1057 \myfun{length} is the usual polymorphic length function. $\myfun{>}$ is
1058 a function that takes two naturals and returns a type: if the lhs is
1059 greater then the rhs, $\myunit$ is returned, $\myempty$ otherwise.  This
1060 way, we can express a `non-emptyness' condition in $\myfun{head}$, by
1061 including a proof that the length of the list argument is non-zero.
1062 This allows us to rule out the `empty list' case, so that we can safely
1063 return the first element.
1064
1065 Again, we need to make sure that the type hierarchy is respected, which is the
1066 reason why a type formed by $\myarr$ will live in the least upper bound of the
1067 levels of argument and return type.  This trend will continue with the other
1068 type-level binders, $\myprod$ and $\mytyc{W}$.
1069
1070 \subsubsection{$\myprod$, or dependent product}
1071 \label{sec:disju}
1072
1073 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1074      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1075      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1076      \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1077      \DisplayProof
1078
1079      \myderivsp
1080
1081     \begin{tabular}{cc}
1082       \AxiomC{$\myjud{\mytmm}{\mytya}$}
1083       \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1084       \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1085       \noLine
1086       \UnaryInfC{\phantom{$--$}}
1087       \DisplayProof
1088       &
1089       \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1090       \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1091       \noLine
1092       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1093       \DisplayProof
1094     \end{tabular}
1095 }
1096
1097 If dependent functions are a generalisation of $\myarr$ in the STLC,
1098 dependent products are a generalisation of $\myprod$ in the STLC.  The
1099 improvement is that the second element's type can depend on the value of
1100 the first element.  The corrispondence with logic is through the
1101 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1102 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1103 The first element will be a number, and the second evidence that the
1104 number is even.  This highlights the fact that we are working in a
1105 constructive logic: if we have an existence proof, we can always ask for
1106 a witness.  This means, for instance, that $\neg \forall \neg$ is not
1107 equivalent to $\exists$.
1108
1109 Another perhaps more `dependent' application of products, paired with
1110 $\mybool$, is to offer choice between different types.  For example we
1111 can easily recover disjunctions:
1112 {\small\[
1113 \begin{array}{l}
1114   \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1115   \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1116   \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} \\
1117   \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1118   \myind{2} \myapp{(\myitee{\myapp{\myfst}{\myb{b}}}{\myb{x}}{(\myite{\myb{b}}{\myb{A}}{\myb{B}})}{\myb{f}}{\myb{g}})}{(\myapp{\mysnd}{\myb{x}})}
1119 \end{array}
1120 \]}
1121
1122 \subsubsection{$\mytyc{W}$, or well-order}
1123 \label{sec:well-order}
1124
1125 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1126      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1127      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1128      \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1129      \DisplayProof
1130
1131      \myderivsp
1132
1133      \AxiomC{$\myjud{\mytmt}{\mytya}$}
1134      \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1135      \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1136      \DisplayProof
1137
1138      \myderivsp
1139
1140      \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1141      \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1142      \noLine
1143      \BinaryInfC{$\myjud{\myse{p}}{
1144        \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}}}}
1145      }$}
1146      \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1147      \DisplayProof
1148 }
1149
1150 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1151 let us represent inductive data in a general (but clumsy) way.  The core
1152 idea is to
1153
1154
1155 \section{The struggle for equality}
1156 \label{sec:equality}
1157
1158 In the previous section we saw how a type checker (or a human) needs a
1159 notion of \emph{definitional equality}.  Beyond this meta-theoretic
1160 notion, in this section we will explore the ways of expressing equality
1161 \emph{inside} the theory, as a reasoning tool available to the user.
1162 This area is the main concern of this thesis, and in general a very
1163 active research topic, since we do not have a fully satisfactory
1164 solution, yet.  As in the previous section, everything presented is
1165 formalised in Agda in appendix \ref{app:agda-itt}.
1166
1167 \subsection{Propositional equality}
1168
1169 \noindent
1170 \begin{minipage}{0.5\textwidth}
1171 \mydesc{syntax}{ }{
1172   $
1173   \begin{array}{r@{\ }c@{\ }l}
1174     \mytmsyn & ::= & \cdots \\
1175              &  |  & \mytmsyn \mypeq{\mytmsyn} \mytmsyn \mysynsep
1176                      \myapp{\myrefl}{\mytmsyn} \\
1177              &  |  & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1178   \end{array}
1179   $
1180 }
1181 \end{minipage} 
1182 \begin{minipage}{0.5\textwidth}
1183 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1184     $
1185     \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1186     $
1187   \vspace{0.87cm}
1188 }
1189 \end{minipage}
1190
1191 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1192     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1193     \AxiomC{$\myjud{\mytmm}{\mytya}$}
1194     \AxiomC{$\myjud{\mytmn}{\mytya}$}
1195     \TrinaryInfC{$\myjud{\mytmm \mypeq{\mytya} \mytmn}{\mytyp_l}$}
1196     \DisplayProof
1197
1198     \myderivsp
1199
1200     \begin{tabular}{cc}
1201       \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1202       \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mytmm \mypeq{\mytya} \mytmn}$}
1203       \DisplayProof
1204       &
1205       \AxiomC{$
1206         \begin{array}{c}
1207           \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\myb{x} \mypeq{\mytya} \myb{y}}{\mytyp_l}}} \\
1208           \myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1209         \end{array}
1210         $}
1211       \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1212       \DisplayProof
1213     \end{tabular}
1214 }
1215
1216 To express equality between two terms inside ITT, the obvious way to do so is
1217 to have the equality construction to be a type-former.  Here we present what
1218 has survived as the dominating form of equality in systems based on ITT up to
1219 the present day.
1220
1221 Our type former is $\mypeq{\mytya}$, which given a type (in this case
1222 $\mytya$) relates equal terms of that type.  $\mypeq{}$ has one introduction
1223 rule, $\myrefl$, which introduces an equality relation between definitionally
1224 equal terms.
1225
1226 Finally, we have one eliminator for $\mypeq{}$, $\myjeqq$.  $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1227 \begin{itemize}
1228 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1229   $\mytya$) and a proof of their equality
1230 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1231   $\myse{n}$) are equal
1232 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$, plus
1233   the trivial proof by reflexivity showing that $\myse{m}$ is equal to itself
1234 \end{itemize}
1235 Given these ingredients, $\myjeqq$ retuns a member of $\myse{P}$ applied to
1236 $\mytmm$, $\mytmn$, and $\myse{q}$.  In other words $\myjeqq$ takes a
1237 witness that $\myse{P}$ works with \emph{definitionally equal} terms, and
1238 returns a witness of $\myse{P}$ working with \emph{propositionally equal}
1239 terms.  Invokations of $\myjeqq$ will vanish when the equality proofs will
1240 reduce to invocations to reflexivity, at which point the arguments must be
1241 definitionally equal, and thus the provided
1242 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1243 can be returned.
1244
1245 While the $\myjeqq$ rule is slightly convoluted, ve can derive many more
1246 `friendly' rules from it, for example a more obvious `substitution' rule, that
1247 replaces equal for equal in predicates:
1248 {\small\[
1249 \begin{array}{l}
1250 \myfun{subst} : \myfora{\myb{A}}{\mytyp}{\myfora{\myb{P}}{\myb{A} \myarr \mytyp}{\myfora{\myb{x}\ \myb{y}}{\myb{A}}{\myb{x} \mypeq{\myb{A}} \myb{y} \myarr \myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{\myb{y}}}}} \\
1251 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1252   \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1253 \end{array}
1254 \]}
1255 Once we have $\myfun{subst}$, we can easily prove more familiar laws regarding
1256 equality, such as symmetry, transitivity, and a congruence law.
1257
1258 % TODO finish this
1259
1260 \subsection{Common extensions}
1261
1262 Our definitional equality can be made larger in various ways, here we
1263 review some common extensions.
1264
1265 \subsubsection{Congruence laws and $\eta$-expansion}
1266
1267 A simple type-directed check that we can do on functions and records is
1268 $\eta$-expansion.  We can then have
1269
1270 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1271   \begin{tabular}{cc}
1272     \AxiomC{$\myjud{f \mydefeq (\myabss{\myb{x}}{\mytya}{\myapp{\myse{g}}{\myb{x}}})}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1273     \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1274     \DisplayProof
1275     &
1276     \AxiomC{$\myjud{\mytmm \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1277     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1278     \DisplayProof
1279   \end{tabular}
1280
1281   \myderivsp
1282
1283   \AxiomC{$\myjud{\mytmm}{\myunit}$}
1284   \AxiomC{$\myjud{\mytmn}{\myunit}$}
1285   \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1286   \DisplayProof
1287 }
1288
1289 %   \mydesc{definitional equality:}{\mytmsyn \mydefeq \mytmsyn}{
1290 %     \begin{tabular}{cc}
1291 %       \AxiomC{}
1292 %       &
1293 %       foo
1294 %     \end{tabular}
1295 %   }
1296 % \end{description}
1297
1298 \subsubsection{Uniqueness of identity proofs}
1299
1300 % TODO reference the fact that J does not imply J
1301 % TODO mention univalence
1302
1303
1304 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1305   \AxiomC{$
1306     \begin{array}{@{}c}
1307       \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\myb{x} \mypeq{\mytya} \myb{x} \myarr \mytyp}} \\\
1308       \myjud{\myse{p}}{\myfora{\myb{x}}{\mytya}{\myse{P} \myappsp \myb{x} \myappsp \myb{x} \myappsp (\myrefl \myapp \myb{x})}} \hspace{1cm}
1309       \myjud{\mytmt}{\mytya} \hspace{1cm}
1310       \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1311     \end{array}
1312     $}
1313   \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{p} \myappsp \myse{t} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1314   \DisplayProof
1315 }
1316
1317 \subsection{Limitations}
1318
1319 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1320   schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1321
1322 However, propositional equality as described is quite restricted when
1323 reasoning about equality beyond the term structure, which is what definitional
1324 equality gives us (extension notwithstanding).
1325
1326 The problem is best exemplified by \emph{function extensionality}.  In
1327 mathematics, we would expect to be able to treat functions that give equal
1328 output for equal input as the same.  When reasoning in a mechanised framework
1329 we ought to be able to do the same: in the end, without considering the
1330 operational behaviour, all functions equal extensionally are going to be
1331 replaceable with one another.
1332
1333 However this is not the case, or in other words with the tools we have we have
1334 no term of type
1335 {\small\[
1336 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1337     \myb{A} \myarr \myb{B}}{
1338         (\myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{\myb{B}} \myapp{\myb{g}}{\myb{x}}}) \myarr
1339         \myb{f} \mypeq{\myb{A} \myarr \myb{B}} \myb{g}
1340     }
1341 }
1342 \]}
1343 To see why this is the case, consider the functions
1344 {\small\[\myabs{\myb{x}}{0 \mathrel{\myfun{+}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{+}} 0}\]}
1345 where $\myfun{+}$ is defined by recursion on the first argument,
1346 gradually destructing it to build up successors of the second argument.
1347 The two functions are clearly extensionally equal, and we can in fact
1348 prove that
1349 {\small\[
1350 \myfora{\myb{x}}{\mynat}{(0 \mathrel{\myfun{+}} \myb{x}) \mypeq{\mynat} (\myb{x} \mathrel{\myfun{+}} 0)}
1351 \]}
1352 By analysis on the $\myb{x}$.  However, the two functions are not
1353 definitionally equal, and thus we won't be able to get rid of the
1354 quantification.
1355
1356 For the reasons above, theories that offer a propositional equality
1357 similar to what we presented are called \emph{intensional}, as opposed
1358 to \emph{extensional}.  Most systems in wide use today (such as Agda,
1359 Coq, and Epigram) are of this kind.
1360
1361 This is quite an annoyance that often makes reasoning awkward to execute.  It
1362 also extends to other fields, for example proving bisimulation between
1363 processes specified by coinduction, or in general proving equivalences based
1364 on the behaviour on a term.
1365
1366 \subsection{Equality reflection}
1367
1368 One way to `solve' this problem is by identifying propositional equality with
1369 definitional equality:
1370
1371 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1372     \AxiomC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}$}
1373     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1374     \DisplayProof
1375 }
1376
1377 This rule takes the name of \emph{equality reflection}, and is a very
1378 different rule from the ones we saw up to now: it links a typing judgement
1379 internal to the type theory to a meta-theoretic judgement that the type
1380 checker uses to work with terms.  It is easy to see the dangerous consequences
1381 that this causes:
1382 \begin{itemize}
1383 \item The rule is syntax directed, and the type checker is presumably expected
1384   to come up with equality proofs when needed.
1385 \item More worryingly, type checking becomes undecidable also because
1386   computing under false assumptions becomes unsafe.
1387   Consider for example
1388   {\small\[
1389   \myabss{\myb{q}}{\mytya \mypeq{\mytyp} (\mytya \myarr \mytya)}{\myhole{?}}
1390   \]}
1391   Using the assumed proof in tandem with equality reflection we could easily
1392   write a classic Y combinator, sending the compiler into a loop.
1393 \end{itemize}
1394
1395 Given these facts theories employing equality reflection, like NuPRL
1396 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1397 to keep the systems manageable.  % TODO more info, problems with that.
1398
1399 For all its faults, equality reflection does allow us to prove extensionality,
1400 using the extensions we gave above.  Assuming that $\myctx$ contains
1401 {\small\[\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}}}\]}
1402 We can then derive
1403 \begin{prooftree}
1404   \small
1405   \AxiomC{$\hspace{1.1cm}\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{q}}{\myb{x}}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\hspace{1.1cm}$}
1406   \RightLabel{equality reflection}
1407   \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1408   \RightLabel{congruence for $\lambda$s}
1409   \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1410   \RightLabel{$\eta$-law for $\lambda$}
1411   \UnaryInfC{$\hspace{1.45cm}\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}\hspace{1.45cm}$}
1412   \RightLabel{$\myrefl$}
1413   \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\myb{f} \mypeq{} \myb{g}}$}
1414 \end{prooftree}
1415
1416 Now, the question is: do we need to give up well-behavedness of our theory to
1417 gain extensionality?
1418
1419 \subsection{Some alternatives}
1420
1421 % TODO add `extentional axioms' (Hoffman), setoid models (Thorsten)
1422
1423 \section{Observational equality}
1424 \label{sec:ott}
1425
1426 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1427   Theory} (OTT), promises to keep the well behavedness of ITT while
1428 being able to gain many useful equality proofs\footnote{It is suspected
1429   that OTT gains \emph{all} the equality proofs of ETT, but no proof
1430   exists yet.}, including function extensionality.  The main idea is to
1431 give the user the possibility to \emph{coerce} (or transport) values
1432 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1433 structurally that $\mytya$ and $\mytya$ are equal; and providing a
1434 value-level equality based on similar principles.  Here we give an
1435 exposition which follows closely the original paper.
1436
1437 \subsection{A simpler theory, a propositional fragment}
1438
1439 \mydesc{syntax}{ }{
1440     $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1441     $
1442     \begin{array}{r@{\ }c@{\ }l}
1443       \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1444                        \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1445       \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1446       \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1447     \end{array}
1448     $
1449 }
1450
1451 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1452   \begin{tabular}{cc}
1453     \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1454     \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1455     \DisplayProof
1456     &
1457     \AxiomC{$\myjud{\mytmt}{\mybool}$}
1458     \AxiomC{$\myjud{\mytya}{\mytyp}$}
1459     \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1460     \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1461     \DisplayProof
1462   \end{tabular}
1463 }
1464
1465 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1466     \begin{tabular}{cc}
1467       \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1468       \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1469       \noLine
1470       \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1471       \DisplayProof
1472       &
1473       \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1474       \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1475       \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1476       \noLine
1477       \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1478       \DisplayProof
1479     \end{tabular}
1480
1481     \myderivsp
1482
1483       \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1484       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1485       \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1486       \DisplayProof
1487 }
1488
1489 Our foundation will be a type theory like the one of section
1490 \ref{sec:itt}, with only one level: $\mytyp_0$.  In this context we will
1491 drop the $0$ and call $\mytyp_0$ $\mytyp$.  Moreover, since the old
1492 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1493 types thanks to the hierarchy (which is gone), we need to reintroduce an
1494 ad-hoc conditional for types, where the reduction rule is the obvious
1495 one.
1496
1497 However, we have an addition: a universe of \emph{propositions},
1498 $\myprop$.  $\myprop$ isolates a fragment of types at large, and
1499 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$: \\
1500 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
1501     \begin{tabular}{cc}
1502     $
1503     \begin{array}{l@{\ }c@{\ }l}
1504       \myprdec{\mybot} & \myred & \myempty \\
1505       \myprdec{\mytop} & \myred & \myunit
1506     \end{array}
1507     $
1508     &
1509     $
1510     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
1511       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
1512       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
1513              \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
1514     \end{array}
1515     $
1516     \end{tabular}
1517   } \\
1518   Propositions are what we call the types of \emph{proofs}, or types
1519   whose inhabitants contain no `data', much like $\myunit$.  The goal of
1520   doing this is twofold: erasing all top-level propositions when
1521   compiling; and to identify all equivalent propositions as the same, as
1522   we will see later.
1523
1524   Why did we choose what we have in $\myprop$?  Given the above
1525   criteria, $\mytop$ obviously fits the bill.  A pair of propositions
1526   $\myse{P} \myand \myse{Q}$ still won't get us data. Finally, if
1527   $\myse{P}$ is a proposition and we have
1528   $\myprfora{\myb{x}}{\mytya}{\myse{P}}$ , the decoding will be a
1529   function which returns propositional content.  The only threat is
1530   $\mybot$, by which we can fabricate anything we want: however if we
1531   are consistent there will be nothing of type $\mybot$ at the top
1532   level, which is what we care about regarding proof erasure.
1533
1534 \subsection{Equality proofs}
1535
1536 \mydesc{syntax}{ }{
1537     $
1538     \begin{array}{r@{\ }c@{\ }l}
1539       \mytmsyn & ::= & \cdots \mysynsep
1540       \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
1541       \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1542       \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
1543       \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
1544     \end{array}
1545     $
1546 }
1547
1548 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1549   \begin{tabular}{cc}
1550     \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1551     \AxiomC{$\myjud{\mytmt}{\mytya}$}
1552     \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
1553     \DisplayProof
1554     &
1555   \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1556   \AxiomC{$\myjud{\mytmt}{\mytya}$}
1557   \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
1558   \DisplayProof
1559
1560   \end{tabular}
1561 }
1562
1563 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1564     \begin{tabular}{cc}
1565       \AxiomC{$
1566         \begin{array}{l}
1567           \ \\
1568           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
1569         \end{array}
1570         $}
1571       \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
1572       \DisplayProof
1573       &
1574       \AxiomC{$
1575         \begin{array}{c}
1576           \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
1577           \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
1578         \end{array}
1579         $}
1580     \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
1581     \DisplayProof
1582
1583     \end{tabular}
1584 }
1585
1586
1587 While isolating a propositional universe as presented can be a useful
1588 exercises on its own, what we are really after is a useful notion of
1589 equality.  In OTT we want to maintain the notion that things judged to
1590 be equal are still always repleaceable for one another with no
1591 additional changes.  Note that this is not the same as saying that they
1592 are definitionally equal, since as we saw extensionally equal functions,
1593 while satisfying the above requirement, are not definitionally equal.
1594
1595 Towards this goal we introduce two equality constructs in
1596 $\myprop$---the fact that they are in $\myprop$ indicates that they
1597 indeed have no computational content.  The first construct, $\myarg
1598 \myeq \myarg$, relates types, the second,
1599 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values.  The
1600 value-level equality is different from our old propositional equality:
1601 instead of ranging over only one type, we might form equalities between
1602 values of different types---the usefulness of this construct will be
1603 clear soon.  In the literature this equality is known as `heterogeneous'
1604 or `John Major', since
1605
1606 \begin{quote}
1607   John Major's `classless society' widened people's aspirations to
1608   equality, but also the gap between rich and poor. After all, aspiring
1609   to be equal to others than oneself is the politics of envy. In much
1610   the same way, forms equations between members of any type, but they
1611   cannot be treated as equals (ie substituted) unless they are of the
1612   same type. Just as before, each thing is only equal to
1613   itself. \citep{McBride1999}.
1614 \end{quote}
1615
1616 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
1617 transport values between equal types; and $\myfun{coh}$ (`coherence')
1618 guarantees that $\myfun{coe}$ respects the value-level equality, or in
1619 other words that it really has no computational component: if we
1620 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
1621 will still be the same.
1622
1623 Before introducing the core ideas that make OTT work, let us distinguish
1624 between \emph{canonical} and \emph{neutral} types.  Canonical types are
1625 those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
1626 and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$).  Neutral
1627 types are those formed by
1628 $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
1629 Correspondingly, canonical terms are those inhabiting canonical types
1630 ($\mytt$, $\mytrue$, $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$,
1631 ...), and neutral terms those formed by eliminators\footnote{Using the
1632   terminology from section \ref{sec:types}, we'd say that canonical
1633   terms are in \emph{weak head normal form}.}.  In the current system
1634 (and hopefully in well-behaved systems), all closed terms reduce to a
1635 canonical term, and all canonical types are inhabited by canonical
1636 terms.
1637
1638 \subsubsection{Type equality, and coercions}
1639
1640 The plan is to decompose type-level equalities between canonical types
1641 into decodable propositions containing equalities regarding the
1642 subterms, and to use coerce recursively on the subterms using the
1643 generated equalities.  This interplay between type equalities and
1644 \myfun{coe} ensures that invocations of $\myfun{coe}$ will vanish when
1645 we have evidence of the structural equality of the types we are
1646 transporting terms across.  If the type is neutral, the equality won't
1647 reduce and thus $\myfun{coe}$ won't reduce either.  If we come an
1648 equality between different canonical types, then we reduce the equality
1649 to bottom, making sure that no such proof can exist, and providing an
1650 `escape hatch' in $\myfun{coe}$.
1651
1652 \begin{figure}[t]
1653
1654 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1655     $
1656       \begin{array}{c@{\ }c@{\ }c@{\ }l}
1657         \myempty & \myeq & \myempty & \myred \mytop \\
1658         \myunit  & \myeq &  \myunit & \myred  \mytop \\
1659         \mybool  & \myeq &  \mybool &   \myred  \mytop \\
1660         \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
1661         \multicolumn{4}{l}{
1662           \myind{2} \mytya_1 \myeq \mytyb_1 \myand 
1663                   \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}]}
1664                   } \\
1665       \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1666       \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1667       \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1668       \end{array}
1669     $
1670 }
1671 \myderivsp
1672 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
1673   $
1674   \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
1675     \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
1676     \mycoe & \myunit  & \myunit  & \myse{Q} & \mytt & \myred & \mytt \\
1677     \mycoe & \mybool  & \mybool  & \myse{Q} & \mytrue & \myred & \mytrue \\
1678     \mycoe & \mybool  & \mybool  & \myse{Q} & \myfalse & \myred & \myfalse \\
1679     \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1680              (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1681              \mytmt_1 & \myred & \\
1682              \multicolumn{7}{l}{
1683              \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
1684                \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
1685                            & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
1686                            & \myb{Q_A}      & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
1687                            & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
1688                            & \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}}} \\
1689                            & \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}} \\
1690                \mysyn{in}  & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
1691               \end{array}} \\
1692
1693     \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1694              (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1695              \mytmt & \myred &
1696            \cdots \\
1697
1698     \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1699              (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1700              \mytmt & \myred &
1701            \cdots \\
1702
1703     \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred &  \\
1704     \multicolumn{7}{l}{
1705       \myind{2}\myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1706     }
1707   \end{array}
1708   $
1709 }
1710 \caption{Reducing type equalities, and using them when
1711   $\myfun{coe}$rcing.}
1712 \label{fig:eqred}
1713 \end{figure}
1714
1715 Figure \ref{fig:eqred} illustrates this idea in practice.  For ground
1716 types, the proof is the trivial element, and \myfun{coe} is the
1717 identity.  For the three type binders, things are similar but subtly
1718 different---the choices we make in the type equality are dictated by
1719 the desire of writing the $\myfun{coe}$ in a natural way.
1720
1721 $\myprod$ is the easiest case: we decompose the proof into proofs that
1722 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
1723 proof that given equal values in the first element, the types of the
1724 second elements are equal too
1725 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
1726   \myimpl \mytyb_1 \myeq \mytyb_2}$)\footnote{We are using $\myimpl$ to
1727   indicate a $\forall$ where we discard the first value.  We write
1728   $\mytyb_1[\myb{x_1}]$ to indicate that the $\myb{x_1}$ in $\mytyb_1$
1729   is re-bound to the $\myb{x_1}$ quantified by the $\forall$, and
1730   similarly for $\myb{x_2}$ and $\mytyb_2$.}.  This also explains the
1731 need for heterogeneous equality, since in the second proof it would be
1732 awkward to express the fact that $\myb{A_1}$ is the same as $\myb{A_2}$.
1733 In the respective $\myfun{coe}$ case, since the types are canonical, we
1734 know at this point that the proof of equality is a pair of the shape
1735 described above.  Thus, we can immediately coerce the first element of
1736 the pair using the first element of the proof, and then instantiate the
1737 second element with the two first elements and a proof by coherence of
1738 their equality, since we know that the types are equal.  The cases for
1739 the other binders are omitted for brevity, but they follow the same
1740 principle.
1741
1742 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
1743
1744 It is important to notice that in the reduction rules for $\myfun{coe}$
1745 are never obstructed by the proofs: with the exception of comparisons
1746 between different canonical types we never pattern match on the pairs,
1747 but always look at the projections.  This means that, as long as we are
1748 consistent, and thus as long as we don't have $\mybot$-inducing proofs,
1749 we can add propositional axioms for equality and $\myfun{coe}$ will
1750 still compute.  Thus, we can take $\myfun{coh}$ as axiomatic, and we can
1751 add back familiar useful equality rules:
1752
1753 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1754   \AxiomC{$\myjud{\mytmt}{\mytya}$}
1755   \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\myb{x}}{\myb{\mytya}}{\myb{x}}{\myb{\mytya}}}}$}
1756   \DisplayProof
1757   
1758   \myderivsp
1759   
1760   \AxiomC{$\myjud{\mytya}{\mytyp}$}
1761   \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
1762   \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}}}}}$}
1763   \DisplayProof
1764 }
1765
1766 $\myrefl$ is the equivalent of the reflexivity rule in propositional
1767 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
1768 abstracting over a value we can substitute equal for equal---this lets
1769 us recover $\myfun{subst}$.  Note that while we need to provide ad-hoc
1770 rules in the restricted, non-hierarchical theory that we have, if our
1771 theory supports abstraction over $\mytyp$s we can easily add these
1772 axioms as abstracted variables.
1773
1774 \subsubsection{Value-level equality}
1775
1776 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1777   $
1778   \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
1779     (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
1780     (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty&) & \myred \mytop \\
1781     (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
1782     (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
1783     (&\mytmt_1 & : & \mybool&) & \myeq & (&\mytmt_1 & : & \mybool&) & \myred \mybot \\
1784     (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1785      & \multicolumn{11}{@{}l}{
1786       \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
1787       \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}}}
1788     } \\
1789    (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1790      & \multicolumn{11}{@{}l}{
1791        \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
1792            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
1793            \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}]}
1794          }}
1795     } \\
1796    (&\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 \\
1797     (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \\
1798     & \multicolumn{11}{@{}l}{
1799       \myind{2} \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
1800     }
1801   \end{array}
1802   $
1803 }
1804
1805 As with type-level equality, we want value-level equality to reduce
1806 based on the structure of the compared terms.
1807
1808 \subsection{Proof irrelevance}
1809
1810 % \section{Augmenting ITT}
1811 % \label{sec:practical}
1812
1813 % \subsection{A more liberal hierarchy}
1814
1815 % \subsection{Type inference}
1816
1817 % \subsubsection{Bidirectional type checking}
1818
1819 % \subsubsection{Pattern unification}
1820
1821 % \subsection{Pattern matching and explicit fixpoints}
1822
1823 % \subsection{Induction-recursion}
1824
1825 % \subsection{Coinduction}
1826
1827 % \subsection{Dealing with partiality}
1828
1829 % \subsection{Type holes}
1830
1831 \section{\mykant : the theory}
1832 \label{sec:kant-theory}
1833
1834 \mykant\ is an interactive theorem prover developed as part of this thesis.
1835 The plan is to present a core language which would be capable of serving as
1836 the basis for a more featureful system, while still presenting interesting
1837 features and more importantly observational equality.
1838
1839 The author learnt the hard way the implementations challenges for such a
1840 project, and while there is a solid and working base to work on, observational
1841 equality is not currently implemented.  However, a detailed plan on how to add
1842 it this functionality is provided, and should not prove to be too much work.
1843
1844 The features currently implemented in \mykant\ are:
1845
1846 \begin{description}
1847 \item[Full dependent types] As we would expect, we have dependent a system
1848   which is as expressive as the `best' corner in the lambda cube described in
1849   section \ref{sec:itt}.
1850
1851 \item[Implicit, cumulative universe hierarchy] The user does not need to
1852   specify universe level explicitly, and universes are \emph{cumulative}.
1853
1854 \item[User defined data types and records] Instead of forcing the user to
1855   choose from a restricted toolbox, we let her define inductive data types,
1856   with associated primitive recursion operators; or records, with associated
1857   projections for each field.
1858
1859 \item[Bidirectional type checking] While no `fancy' inference via unification
1860   is present, we take advantage of an type synthesis system in the style of
1861   \cite{Pierce2000}, extending the concept for user defined data types.
1862
1863 \item[Type holes] When building up programs interactively, it is useful to
1864   leave parts unfinished while exploring the current context.  This is what
1865   type holes are for.
1866 \end{description}
1867
1868 The planned features are:
1869
1870 \begin{description}
1871 \item[Observational equality] As described in section \ref{sec:ott} but
1872   extended to work with the type hierarchy and to admit equality between
1873   arbitrary data types.
1874
1875 \item[Coinductive data] ...
1876 \end{description}
1877
1878 We will analyse the features one by one, along with motivations and tradeoffs
1879 for the design decisions made.
1880
1881 \subsection{Bidirectional type checking}
1882
1883 We start by describing bidirectional type checking since it calls for fairly
1884 different typing rules that what we have seen up to now.  The idea is to have
1885 two kind of terms: terms for which a type can always be inferred, and terms
1886 that need to be checked against a type.  A nice observation is that this
1887 duality runs through the semantics of the terms: data destructors (function
1888 application, record projections, primitive re cursors) \emph{infer} types,
1889 while data constructors (abstractions, record/data types data constructors)
1890 need to be checked.  In the literature these terms are respectively known as
1891
1892 To introduce the concept and notation, we will revisit the STLC in a
1893 bidirectional style.  The presentation follows \cite{Loh2010}.
1894
1895 % TODO do this --- is it even necessary
1896
1897 % The syntax of 
1898
1899 \subsection{Base terms and types}
1900
1901 Let us begin by describing the primitives available without the user
1902 defining any data types, and without equality.  The way we handle
1903 variables and substitution is left unspecified, and explained in section
1904 \ref{sec:term-repr}, along with other implementation issues.  We are
1905 also going to give an account of the implicit type hierarchy separately
1906 in section \ref{sec:term-hierarchy}, so as not to clutter derivation
1907 rules too much, and just treat types as impredicative for the time
1908 being.
1909
1910 \mydesc{syntax}{ }{
1911   $
1912   \begin{array}{r@{\ }c@{\ }l}
1913     \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
1914     &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1915     \myabs{\myb{x}}{\mytmsyn} \mysynsep
1916     (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
1917     (\myann{\mytmsyn}{\mytmsyn}) \\
1918     \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
1919   \end{array}
1920   $
1921 }
1922
1923 The syntax for our calculus includes just two basic constructs:
1924 abstractions and $\mytyp$s.  Everything else will be provided by
1925 user-definable constructs.  Since we let the user define values, we will
1926 need a context capable of carrying the body of variables along with
1927 their type.  Bound names and defined names are treated separately in the
1928 syntax, and while both can be associated to a type in the context, only
1929 defined names can be associated with a body:
1930
1931 \mydesc{context validity:}{\myvalid{\myctx}}{
1932     \begin{tabular}{ccc}
1933       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1934       \UnaryInfC{$\myvalid{\myemptyctx}$}
1935       \DisplayProof
1936       &
1937       \AxiomC{$\myjud{\mytya}{\mytyp}$}
1938       \AxiomC{$\mynamesyn \not\in \myctx$}
1939       \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
1940       \DisplayProof
1941       &
1942       \AxiomC{$\myjud{\mytmt}{\mytya}$}
1943       \AxiomC{$\myfun{f} \not\in \myctx$}
1944       \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
1945       \DisplayProof
1946     \end{tabular}
1947 }
1948
1949 Now we can present the reduction rules, which are unsurprising.  We have
1950 the usual function application ($\beta$-reduction), but also a rule to
1951 replace names with their bodies ($\delta$-reduction), and one to discard
1952 type annotations.  For this reason reduction is done in-context, as
1953 opposed to what we have seen in the past:
1954
1955 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
1956     \begin{tabular}{ccc}
1957       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1958       \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
1959                   \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
1960       \DisplayProof
1961       &
1962       \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
1963       \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
1964       \DisplayProof
1965       &
1966       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1967       \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
1968       \DisplayProof
1969     \end{tabular}
1970 }
1971
1972 We can now give types to our terms.  The type of names, both defined and
1973 abstract, is inferred.  The type of applications is inferred too,
1974 propagating types down the applied term.  Abstractions are checked.
1975 Finally, we have a rule to check the type of an inferrable term.  We
1976 defer the question of term equality (which is needed for type checking)
1977 to section \label{sec:kant-irr}.
1978
1979 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{   
1980     \begin{tabular}{ccc}
1981       \AxiomC{$\myse{name} : A \in \myctx$}
1982       \UnaryInfC{$\myinf{\myse{name}}{A}$}
1983       \DisplayProof
1984       &
1985       \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
1986       \UnaryInfC{$\myinf{\myfun{f}}{A}$}
1987       \DisplayProof
1988       &
1989       \AxiomC{$\myinf{\mytmt}{\mytya}$}
1990       \UnaryInfC{$\mychk{\myann{\mytmt}{\mytya}}{\mytya}$}
1991       \DisplayProof
1992     \end{tabular}
1993     \myderivsp
1994
1995     \begin{tabular}{ccc}
1996       \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1997       \AxiomC{$\mychk{\mytmn}{\mytya}$}
1998       \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1999       \DisplayProof
2000
2001       &
2002
2003       \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
2004       \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2005       \DisplayProof
2006     \end{tabular}
2007 }
2008
2009 \subsection{Elaboration}
2010
2011 As we mentioned, $\mykant$\ allows the user to define not only values
2012 but also custom data types and records.  \emph{Elaboration} consists of
2013 turning these declarations into workable syntax, types, and reduction
2014 rules.  The treatment of custom types in $\mykant$\ is heavily inspired
2015 by McBride and McKinna early work on Epigram \citep{McBride2004},
2016 although with some differences.
2017
2018 \subsubsection{Term vectors, telescopes, and assorted notation}
2019
2020 We use a vector notation to refer to a series of term applied to
2021 another, for example $\mytyc{D} \myappsp \vec{A}$ is a shorthand for
2022 $\mytyc{D} \myappsp \mytya_1 \cdots \mytya_n$, for some $n$.  $n$ is
2023 consistently used to refer to the length of such vectors, and $i$ to
2024 refer to an index in such vectors.  We also often need to `build up'
2025 terms vectors, in which case we use $\myemptyctx$ for an empty vector
2026 and add elements to an existing vector with $\myarg ; \myarg$, similarly
2027 to what we do for context.
2028
2029 To present the elaboration and operations on user defined data types, we
2030 frequently make use what de Bruijn called \emph{telescopes}
2031 \citep{Bruijn91}, a construct that will prove useful when dealing with
2032 the types of type and data constructors.  A telescope is a series of
2033 nested typed bindings, such as $(\myb{x} {:} \mynat); (\myb{p} :
2034 \myapp{\myfun{even}}{\myb{x}})$.  Consistently with the notation for
2035 contexts and term vectors, we use $\myemptyctx$ to denote an empty
2036 telescope and $\myarg ; \myarg$ to add a new binding to an existing
2037 telescope.
2038
2039 We refer to telescopes with $\mytele$, $\mytele'$, $\mytele_i$, etc.  If
2040 $\mytele$ refers to a telescope, $\mytelee$ refers to the term vector
2041 made up of all the variables bound by $\mytele$.  $\mytele \myarr
2042 \mytya$ refers to the type made by turning the telescope into a series
2043 of $\myarr$.  Returning to the examples above, we have that
2044 {\small\[
2045    (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2046    (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2047 \]}
2048
2049 We make use of various operations to manipulate telescopes:
2050 \begin{itemize}
2051 \item $\myhead(\mytele)$ refers to the first type appearing in
2052   $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2053   \myapp{\myfun{even}}{\myb{x}})) = \mynat$.  Similarly,
2054   $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2055   (1-indexed).
2056 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2057   first $i$ elements of $\mytele$:  $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2058   \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$
2059 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2060   terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2061   \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2062   \myapp{\myfun{even}}{42})$.
2063 \end{itemize}
2064
2065 \subsubsection{Declarations syntax}
2066
2067 \mydesc{syntax}{ }{
2068   $
2069   \begin{array}{r@{\ }c@{\ }l}
2070       \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2071                  &  |  & \mypost{\myb{x}}{\mytmsyn} \\
2072                  &  |  & \myadt{\mytyc{D}}{\mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2073                  &  |  & \myreco{\mytyc{D}}{\mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2074
2075       \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2076       \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2077   \end{array}
2078   $
2079 }
2080
2081 In \mykant\ we have four kind of declarations:
2082
2083 \begin{description}
2084 \item[Defined value] A variable, together with a type and a body.
2085 \item[Abstract variable] An abstract variable, with a type but no body.
2086 \item[Inductive data] A datatype, with a type constructor and various data
2087   constructors---somewhat similar to what we find in Haskell.  A primitive
2088   recursor (or `destructor') will be generated automatically.
2089 \item[Record] A record, which consists of one data constructor and various
2090   fields, with no recursive occurrences.
2091 \end{description}
2092
2093 Elaborating defined variables consists of type checking body against the
2094 given type, and updating the context to contain the new binding.
2095 Elaborating abstract variables and abstract variables consists of type
2096 checking the type, and updating the context with a new typed variable:
2097
2098 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2099     \begin{tabular}{cc}
2100       \AxiomC{$\myjud{\mytmt}{\mytya}$}
2101       \AxiomC{$\myfun{f} \not\in \myctx$}
2102       \BinaryInfC{
2103         $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \  \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2104       }
2105       \DisplayProof
2106       &
2107       \AxiomC{$\myjud{\mytya}{\mytyp}$}
2108       \AxiomC{$\myfun{f} \not\in \myctx$}
2109       \BinaryInfC{
2110         $
2111           \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2112           \ \ \myelabf\ \  \myctx; \myfun{f} : \mytya
2113         $
2114       }
2115       \DisplayProof
2116     \end{tabular}
2117 }
2118
2119 \subsubsection{User defined types}
2120 \label{sec:user-type}
2121
2122 \begin{figure}[p]
2123   \begin{subfigure}[b]{\textwidth}
2124     \vspace{-1cm}
2125     \mydesc{syntax}{ }{
2126       \footnotesize
2127       $
2128       \begin{array}{l}
2129         \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2130       \end{array}
2131       $
2132     }
2133
2134   \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
2135     \footnotesize
2136       $
2137       \begin{array}{r@{\ }l}
2138          & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
2139         \myelabf &
2140         
2141         \begin{array}{r@{\ }c@{\ }l}
2142           \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
2143           \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
2144         \end{array}
2145       \end{array}
2146       $
2147   }
2148
2149   \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2150         \footnotesize
2151
2152       \AxiomC{$
2153         \begin{array}{c}
2154           \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2155           \mytyc{D} \not\in \myctx \\
2156           \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
2157           \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
2158         \end{array}
2159           $}
2160       \UnaryInfC{$
2161         \begin{array}{r@{\ }c@{\ }l}
2162           \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
2163           & & \vspace{-0.2cm} \\
2164           & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
2165           &          &
2166           \begin{array}{@{}r@{\ }l l}
2167             \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
2168             & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
2169             & \left.
2170               \begin{array}{@{}l}
2171                 \myind{3} \vdots \\
2172                 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
2173               \end{array} \right \}
2174             & \textbf{methods}  \\
2175             & \myapp{\myb{P}}{\myb{x}} &
2176           \end{array}
2177         \end{array}
2178         $}
2179       \DisplayProof \\ \vspace{0.2cm}\ \\
2180       $
2181         \begin{array}{@{}l l@{\ } l@{} r c l}
2182           \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2183           & \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) \\
2184           & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
2185         \end{array}
2186         $
2187
2188   }
2189
2190   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{  
2191         \footnotesize
2192         $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
2193       \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2194       \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
2195       \BinaryInfC{$
2196           \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)}
2197         $}
2198       \DisplayProof \\ \vspace{0.2cm}\ \\
2199       $
2200         \begin{array}{@{}l l@{\ } l@{} r c l}
2201           \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2202                          & \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) \\
2203                          & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
2204         \end{array}
2205         $
2206   }
2207   \end{subfigure}
2208
2209   \begin{subfigure}[b]{\textwidth}
2210     \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
2211           \footnotesize
2212     $
2213     \begin{array}{r@{\ }c@{\ }l}
2214       \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2215              & \myelabf &
2216
2217              \begin{array}{r@{\ }c@{\ }l}
2218                \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 \\
2219              \end{array}
2220     \end{array}
2221     $
2222 }
2223
2224
2225 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2226       \footnotesize
2227     \AxiomC{$
2228       \begin{array}{c}
2229         \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2230         \mytyc{D} \not\in \myctx \\
2231         \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
2232       \end{array}
2233         $}
2234     \UnaryInfC{$
2235       \begin{array}{r@{\ }c@{\ }l}
2236         \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2237         & & \vspace{-0.2cm} \\
2238         & \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}; \\
2239         & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
2240       \end{array}
2241       $}
2242     \DisplayProof
2243 }
2244
2245   \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2246         \footnotesize
2247           $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
2248           \AxiomC{$\mytyc{D} \in \myctx$}
2249           \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
2250           \DisplayProof
2251   }
2252
2253   \end{subfigure}
2254   \caption{Elaboration for data types and records.}
2255   \label{fig:elab}
2256 \end{figure}
2257
2258 Elaborating user defined types is the real effort.  First, let's explain
2259 what we can defined, with some examples.
2260
2261 \begin{description}
2262 \item[Natural numbers] To define natural numbers, we create a data type
2263   with two constructors: one with zero arguments ($\mydc{zero}$) and one
2264   with one recursive argument ($\mydc{suc}$):
2265   {\small\[
2266   \begin{array}{@{}l}
2267     \myadt{\mynat}{ }{ }{
2268       \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2269     }
2270   \end{array}
2271   \]}
2272   This is very similar to what we would write in Haskell:
2273   {\small\[\text{\texttt{data Nat = Zero | Suc Nat}}\]}
2274   Once the data type is defined, $\mykant$\ will generate syntactic
2275   constructs for the type and data constructors, so that we will have
2276   \begin{center}
2277     \small
2278     \begin{tabular}{ccc}
2279       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2280       \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2281       \DisplayProof
2282     &
2283       \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2284       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2285       \DisplayProof
2286     &
2287       \AxiomC{$\mychk{\mytmt}{\mynat}$}
2288       \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2289       \DisplayProof
2290     \end{tabular}
2291   \end{center}
2292   While in Haskell (or indeed in Agda or Coq) data constructors are
2293   treated the same way as functions, in $\mykant$\ they are syntax, so
2294   for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will be a
2295   syntax error.  This is necessary so that we can easily infer the type
2296   of polymorphic data constructors, as we will see later.
2297
2298   Moreover, each data constructor is prefixed by the type constructor
2299   name, since we need to retrieve the type constructor of a data
2300   constructor when type checking.  This measure aids in the presentation
2301   of various features but it is not needed in the implementation, where
2302   we can have a dictionary to lookup the type constructor corresponding
2303   to each data constructor.  When using data constructors in examples I
2304   will omit the type constructor prefix for brevity.
2305
2306   Along with user defined constructors, $\mykant$\ automatically
2307   generates an \emph{eliminator}, or \emph{destructor}, to compute with
2308   natural numbers: If we have $\mytmt : \mynat$, we can destruct
2309   $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2310   \begin{prooftree}
2311     \small
2312     \AxiomC{$\mychk{\mytmt}{\mynat}$}
2313     \UnaryInfC{$
2314       \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2315         \begin{array}{@{}l}
2316           \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}}
2317           \end{array}
2318         }$}
2319   \end{prooftree}
2320   $\mynat.\myfun{elim}$ corresponds to the induction principle for
2321   natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2322   know that predicate holds for the base case
2323   ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2324   ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2325     \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2326   holds for any number.  As with the data constructors, we require the
2327   eliminator to be applied to the `destructed' element.
2328
2329   While the induction principle is usually seen as a mean to prove
2330   properties about numbers, in the intuitionistic setting it is also a
2331   mean to compute.  In this specific case we will $\mynat.\myfun{elim}$
2332   will return the base case if the provided number is $\mydc{zero}$, and
2333   recursively apply the inductive step if the number is a
2334   $\mydc{suc}$cessor:
2335   {\small\[
2336   \begin{array}{@{}l@{}l}
2337     \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2338     \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})
2339   \end{array}
2340   \]}
2341   The Haskell equivalent would be
2342   {\small\[
2343     \begin{array}{@{}l}
2344       \text{\texttt{elim :: Nat -> a -> (Nat -> a -> a) -> a}}\\
2345       \text{\texttt{elim Zero    pz ps = pz}}\\
2346       \text{\texttt{elim (Suc n) pz ps = ps n (elim n pz ps)}}
2347     \end{array}
2348     \]}
2349   Which buys us the computational behaviour, but not the reasoning power.
2350   % TODO maybe more examples, e.g. Haskell eliminator and fibonacci
2351
2352 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2353   lists are too similar to natural numbers to be interesting.
2354   {\small\[
2355   \begin{array}{@{}l}
2356     \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2357       \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2358     }
2359   \end{array}
2360   \]}
2361   Now the purpose of constructors as syntax can be explained: what would
2362   the type of $\mydc{leaf}$ be?  If we were to treat it as a `normal'
2363   term, we would have to specify the type parameter of the tree each
2364   time the constructor is applied:
2365   {\small\[
2366   \begin{array}{@{}l@{\ }l}
2367     \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2368     \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2369   \end{array}
2370   \]}
2371   The problem with this approach is that creating terms is incredibly
2372   verbose and dull, since we would need to specify the type parameters
2373   each time.  For example if we wished to create a $\mytree \myappsp
2374   \mynat$ with two nodes and three leaves, we would have to write
2375   {\small\[
2376   \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)
2377   \]}
2378   The redundancy of $\mynat$s is quite irritating.  Instead, if we treat
2379   constructors as syntactic elements, we can `extract' the type of the
2380   parameter from the type that the term gets checked against, much like
2381   we get the type of abstraction arguments:
2382   \begin{center}
2383     \small
2384     \begin{tabular}{cc}
2385       \AxiomC{$\mychk{\mytya}{\mytyp}$}
2386       \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2387       \DisplayProof
2388       &
2389       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2390       \AxiomC{$\mychk{\mytmt}{\mytya}$}
2391       \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2392       \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2393       \DisplayProof
2394     \end{tabular}
2395   \end{center}
2396   Which enables us to write, much more concisely
2397   {\small\[
2398   \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}
2399   \]}
2400   We gain an annotation, but we lose the myriad of types applied to the
2401   constructors.  Conversely, with the eliminator for $\mytree$, we can
2402   infer the type of the arguments given the type of the destructed:
2403   \begin{prooftree}
2404     \footnotesize
2405     \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2406     \UnaryInfC{$
2407       \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2408         \begin{array}{@{}l}
2409           (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2410           \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2411           ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2412           \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2413           \myapp{\myb{P}}{\mytmt}
2414         \end{array}
2415       }
2416       $}
2417   \end{prooftree}
2418   As expected, the eliminator embodies structural induction on trees.
2419
2420 \item[Empty type] We have presented types that have at least one
2421   constructors, but nothing prevents us from defining types with
2422   \emph{no} constructors:
2423   {\small\[
2424   \myadt{\mytyc{Empty}}{ }{ }{ }
2425   \]}
2426   What shall the `induction principle' on $\mytyc{Empty}$ be?  Does it
2427   even make sense to talk about induction on $\mytyc{Empty}$?
2428   $\mykant$\ does not care, and generates an eliminator with no `cases',
2429   and thus corresponding to the $\myfun{absurd}$ that we know and love:
2430   \begin{prooftree}
2431     \small
2432     \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2433     \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2434   \end{prooftree}
2435
2436 \item[Ordered lists] Up to this point, the examples shown are nothing
2437   new to the \{Haskell, SML, OCaml, functional\} programmer.  However
2438   dependent types let us express much more than 
2439   % TODO
2440
2441 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
2442   us another way to define types: $\mysyn{record}$.  A record is a
2443   datatype with one constructor and `projections' to extract specific
2444   fields of the said constructor.
2445
2446   For example, we can recover dependent products:
2447   {\small\[
2448   \begin{array}{@{}l}
2449     \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}}}
2450   \end{array}
2451   \]}
2452   Here $\myfst$ and $\mysnd$ are the projections, with their respective
2453   types.  Note that each field can refer to the preceding fields.  A
2454   constructor will be automatically generated, under the name of
2455   $\mytyc{Prod}.\mydc{constr}$.  Dually to data types, we will omit the
2456   type constructor prefix for record projections.
2457
2458   Following the bidirectionality of the system, we have that projections
2459   (the destructors of the record) infer the type, while the constructor
2460   gets checked:
2461   \begin{center}
2462     \small
2463     \begin{tabular}{cc}
2464       \AxiomC{$\mychk{\mytmm}{\mytya}$}
2465       \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
2466       \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2467       \noLine
2468       \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
2469       \DisplayProof
2470       &
2471       \AxiomC{$\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2472       \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
2473       \noLine
2474       \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
2475       \DisplayProof
2476     \end{tabular}
2477   \end{center}
2478   What we have is equivalent to ITT's dependent products.
2479 \end{description}
2480
2481 Following the intuition given by the examples, the mechanised
2482 elaboration is presented in figure \ref{fig:elab}, which is essentially
2483 a modification of figure 9 of \citep{McBride2004}\footnote{However, our
2484   datatypes do not have indices, we do bidirectional typechecking by
2485   treating constructors/destructors as syntactic constructs, and we have
2486   records.}.
2487
2488 In data types declarations we allow recursive occurrences as long as
2489 they are \emph{strictly positive}, employing a syntactic check to make
2490 sure that this is the case.  See \cite{Dybjer1991} for a more formal
2491 treatment of inductive definitions in ITT.
2492
2493 For what concerns records, recursive occurrences are disallowed.  The
2494 reason for this choice is answered by the reason for the choice of
2495 having records at all: we need records to give the user types with
2496 $\eta$-laws for equality, as we saw in section % TODO add section
2497 and in the treatment of OTT in section \ref{sec:ott}.  If we tried to
2498 $\eta$-expand recursive data types, we would expand forever.
2499
2500 To implement bidirectional type checking for constructors and
2501 destructors, we store their types in full in the context, and then
2502 instantiate when due:
2503
2504 \mydesc{typing:}{ }{
2505     \AxiomC{$
2506       \begin{array}{c}
2507         \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
2508         \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
2509         \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
2510         \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
2511         \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \ 
2512           (1 \le i \le \mytele'')
2513       \end{array}
2514       $}
2515     \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
2516     \DisplayProof
2517
2518     \myderivsp
2519
2520     \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2521     \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
2522       \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
2523     \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
2524     \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
2525         \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
2526         \myse{F})(\vec{A};\mytmt)}$}
2527     \DisplayProof
2528   }
2529
2530 \subsubsection{Why user defined types?}
2531
2532 % TODO reference levitated theories, indexed containers
2533
2534 foobar
2535
2536 \subsection{Cumulative hierarchy and typical ambiguity}
2537 \label{sec:term-hierarchy}
2538
2539 A type hierarchy as presented in section \label{sec:itt} is a
2540 considerable burden on the user, on various levels.  Consider for
2541 example how we recovered disjunctions in section \ref{sec:disju}: we
2542 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
2543 What if we wanted to form a disjunction containing two $\mytyp_0$, or
2544 $\mytyp_{42}$?  Our definition would fail us, since $\mytyp_0 :
2545 \mytyp_1$.
2546
2547 One way to solve this issue is a \emph{cumulative} hierarchy, where
2548 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$.  This way we retain
2549 consistency, while allowing for `large' definitions that work on small
2550 types too.  For example we might define our disjunction to be
2551 {\small\[
2552   \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
2553 \]}
2554 And hope that $\mytyp_{100}$ will be large enough to fit all the types
2555 that we want to use with our disjunction.  However, there are two
2556 problems with this.  First, there is the obvious clumsyness of having to
2557 manually specify the size of types.  More importantly, if we want to use
2558 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
2559 make sure that those allow for types at least as large as
2560 $\mytyp_{100}$.
2561
2562 A better option is to employ a mechanised version of what Russell called
2563 \emph{typical ambiguity}: we let the user live under the illusion that
2564 $\mytyp : \mytyp$, but check that the statements about types are
2565 consistent behind the hood.  $\mykant$\ implements this following the
2566 lines of \cite{Huet1988}.  See also \citep{Harper1991} for a published
2567 reference, although describing a more complex system allowing for both
2568 explicit and explicit hierarchy at the same time.
2569
2570 We define a partial ordering on the levels, with both weak ($\le$) and
2571 strong ($<$) constraints---the laws governing them being the same as the
2572 ones governing $<$ and $\le$ for the natural numbers.  Each occurrence
2573 of $\mytyp$ is decorated with a unique reference, and we keep a set of
2574 constraints and add new constraints as we type check, generating new
2575 references when needed.
2576
2577 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
2578 denotes the unique reference assigned to that term, we will generate a
2579 new fresh reference $\mytyp\, r_2$, and add the constraint $r_1 < r_2$
2580 to the set.  When type checking $\myctx \vdash
2581 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
2582 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
2583 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
2584 set.
2585
2586 If at any point the constraint set becomes inconsistent, type checking
2587 fails.  Moreover, when comparing two $\mytyp$ terms we equate their
2588 respective references with two $\le$ constraints---the details are
2589 explained in section \ref{sec:hier-impl}.
2590
2591 Another more flexible but also more verbose alternative is the one
2592 chosen by Agda, where levels can be quantified so that the relationship
2593 between arguments and result in type formers can be explicitly
2594 expressed:
2595 {\small\[
2596 \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}
2597 \]}
2598 Inference algorithms to automatically derive this kind of relationship
2599 are currently subject of research.  We chose less flexible but more
2600 concise way, since it is easier to implement and better understood.
2601
2602 \subsection{Observational equality, \mykant\ style}
2603
2604 There are two correlated differences between $\mykant$\ and the theory
2605 used to present OTT.  The first is that in $\mykant$ we have a type
2606 hierarchy, which lets us, for example, abstract over types.  The second
2607 is that we let the user define inductive types.
2608
2609 Reconciling propositions for OTT and a hierarchy had already been
2610 investigated by Conor McBride\footnote{See
2611   \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.}, and we
2612 follow his footsteps.  Most of the work, as an extension of elaboration,
2613 is to generate reduction rules and coercions.
2614
2615 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
2616
2617 Before defining $\myprop$, we define some basic types inside $\mykant$,
2618 as the target for the $\myprop$ decoder:
2619 \begin{framed}
2620 \small
2621 $
2622 \begin{array}{l}
2623   \myadt{\mytyc{Empty}}{}{ }{ } \\
2624   \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
2625   \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
2626   \ \\
2627
2628   \myreco{\mytyc{Unit}}{}{}{ } \\ \ \\
2629
2630   \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
2631 \end{array}
2632 $
2633 \end{framed}
2634 When using $\mytyc{Prod}$, we shall use $\myprod$ to define `nested'
2635 products, and $\myproj{n}$ to project elements from them, so that
2636 {\small
2637 \[
2638 \begin{array}{@{}l}
2639 \mytya \myprod \mytyb = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myunit) \\
2640 \mytya \myprod \mytyb \myprod \myse{C} = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp (\mytyc{Prod} \myappsp \mytyc \myappsp \myunit)) \\
2641 \myind{2} \vdots \\
2642 \myproj{1} : \mytyc{Prod} \myappsp \mytya \myappsp \mytyb \myarr \mytya \\
2643 \myproj{2} : \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myse{C}) \myarr \mytyb \\
2644 \myind{2} \vdots
2645 \end{array}
2646 \]
2647 }
2648 And so on, so that $\myproj{n}$ will work with all products with at
2649 least than $n$ elements.  Then we can define propositions, and decoding:
2650
2651 \mydesc{syntax}{ }{
2652   $
2653   \begin{array}{r@{\ }c@{\ }l}
2654     \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
2655     \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
2656   \end{array}
2657   $
2658 }
2659
2660 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2661   \begin{tabular}{cc}
2662     $
2663     \begin{array}{l@{\ }c@{\ }l}
2664       \myprdec{\mybot} & \myred & \myempty \\
2665       \myprdec{\mytop} & \myred & \myunit
2666     \end{array}
2667     $
2668     &
2669     $
2670     \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2671       \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2672       \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2673       \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2674     \end{array}
2675     $
2676   \end{tabular}
2677 }
2678
2679 \subsubsection{Why $\myprop$?}
2680
2681 It is worth to ask if $\myprop$ is needed at all.  It is perfectly
2682 possible to have the type checker identify propositional types
2683 automatically, and in fact that is what The author initially planned to
2684 identify the propositional fragment iinternally \cite{Jacobs1994}.
2685
2686 \subsubsection{OTT constructs}
2687
2688 Before presenting the direction that $\mykant$\ takes, let's consider
2689 some examples of use-defined data types, and the result we would expect,
2690 given what we already know about OTT, assuming the same propositional
2691 equalities.
2692
2693 \begin{description}
2694
2695 \item[Product types] Let's consider first the already mentioned
2696   dependent product, using the alternate name $\mysigma$\footnote{For
2697     extra confusion, `dependent products' are often called `dependent
2698     sums' in the literature, referring to the interpretation that
2699     identifies the first element as a `tag' deciding the type of the
2700     second element, which lets us recover sum types (disjuctions), as we
2701     saw in section \ref{sec:user-type}.  Thus, $\mysigma$.} to
2702   avoid confusion with the $\mytyc{Prod}$ in the prelude: {\small\[
2703   \begin{array}{@{}l}
2704     \myreco{\mysigma}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
2705   \end{array}
2706   \]} Let's start with type-level equality.  The result we want is
2707   {\small\[
2708     \begin{array}{@{}l}
2709       \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\
2710       \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}}}
2711     \end{array}
2712     \]} The difference here is that in the original presentation of OTT
2713   the type binders are explicit, while here $\mytyb_1$ and $\mytyb_2$
2714   functions returning types.  We can do this thanks to the type
2715   hierarchy, and this hints at the fact that heterogeneous equality will
2716   have to allow $\mytyp$ `to the right of the colon', and in fact this
2717   provides the solution to simplify the equality above.
2718
2719   If we take, just like we saw previously in OTT
2720   {\small\[
2721     \begin{array}{@{}l}
2722       \myjm{\myse{f}_1}{\myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}}{\myse{f}_2}{\myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}} \myred \\
2723       \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
2724            \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
2725            \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}]}
2726          }}
2727     \end{array}
2728     \]} Then we can simply take
2729   {\small\[
2730     \begin{array}{@{}l}
2731       \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}
2732     \end{array}
2733     \]} Which will reduce to precisely what we desire.  For what
2734   concerns coercions and quotation, things stay the same (apart from the
2735   fact that we apply to the second argument instead of substituting).
2736   We can recognise records such as $\mysigma$ as such and employ
2737   projections in value equality, coercions, and quotation; as to not
2738   impede progress if not necessary.
2739
2740 \item[Lists] Now for finite lists, which will give us a taste for data
2741   constructors:
2742   {\small\[
2743   \begin{array}{@{}l}
2744     \myadt{\mylist}{\myappsp (\myb{A} {:} \mytyp)}{ }{\mydc{nil} \mydcsep \mydc{cons} \myappsp \myb{A} \myappsp (\myapp{\mylist}{\myb{A}})}
2745   \end{array}
2746   \]}
2747   Type equality is simple---we only need to compare the parameter:
2748   {\small\[
2749     \mylist \myappsp \mytya_1 \myeq \mylist \myappsp \mytya_2 \myred \mytya_1 \myeq \mytya_2
2750     \]} For coercions, we transport based on the constructor, recycling
2751   the proof for the inductive occurrence: {\small\[
2752     \begin{array}{@{}l@{\ }c@{\ }l}
2753       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mydc{nil} & \myred & \mydc{nil} \\
2754       \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp (\mydc{cons} \myappsp \mytmm \myappsp \mytmn) & \myred & \\
2755       \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)}
2756     \end{array}
2757     \]} Value equality is unsurprising---we match the constructors, and
2758   return bottom for mismatches.  However, we also need to equate the
2759   parameter in $\mydc{nil}$: {\small\[
2760     \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
2761       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mytya_1 \myeq \mytya_2 \\
2762       (& \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 \\
2763       & \multicolumn{11}{@{}l}{ \myind{2}
2764         \myjm{\mytmm_1}{\mytya_1}{\mytmm_2}{\mytya_2} \myand \myjm{\mytmn_1}{\myapp{\mylist}{\mytya_1}}{\mytmn_2}{\myapp{\mylist}{\mytya_2}}
2765         } \\
2766       (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot \\
2767       (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot
2768     \end{array}
2769     \]}
2770   Finally, quotation
2771   % TODO quotation
2772   
2773
2774 \end{description}
2775   
2776
2777 \mydesc{syntax}{ }{
2778   $
2779   \begin{array}{r@{\ }c@{\ }l}
2780     \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
2781                      \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2782     \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2783   \end{array}
2784   $
2785 }
2786
2787 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
2788   \footnotesize
2789   \AxiomC{}
2790   \UnaryInfC{$
2791     \begin{array}{r@{\ }l}
2792     \myctx \vdash &
2793     \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp}  \myred \\
2794     & \myind{2} \mytya_2 \myeq \mytya_1 \myand \\
2795     & \myind{2} \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
2796         \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1 \myeq \mytyb_2
2797       }}
2798     \end{array}
2799     $}
2800   \DisplayProof
2801
2802   \myderivsp
2803
2804   \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2805   \UnaryInfC{$
2806     \begin{array}{r@{\ }l}
2807       \myctx \vdash &
2808       \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp}  \myred \\
2809       & \myind{2} \myjm{\mytya_1}{\myhead(\mytele)}{\mytyb_1}{\myhead(\mytele)} \myand \cdots \myand \\
2810       & \myind{2} \myjm{\mytya_n}{\myhead(\mytele(A_1 \cdots A_{n-1}))}{\mytyb_n}{\myhead(\mytele(B_1 \cdots B_{n-1}))}
2811     \end{array}
2812     $}
2813   \DisplayProof
2814
2815   \myderivsp
2816
2817   \AxiomC{}
2818   \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
2819   \DisplayProof
2820
2821   \myderivsp
2822
2823   \AxiomC{$
2824     \begin{array}{c}
2825       \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
2826       \mytyc{D}.\mydc{c}_i : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \\
2827       \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
2828       \mytele_B = (\mytele;\mytele')\vec{B}
2829     \end{array}
2830     $}
2831   \UnaryInfC{$
2832     \begin{array}{l}
2833       \myctx \vdash \myjm{\mytyc{D}.\mydc{c}_i \myappsp \vec{\mytmm}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c}_i \myappsp \vec{\mytmn}}{\mytyc{D} \myappsp \vec{B}} \myred \\
2834       \myind{2} \myjm{\mytmm_1}{\myhead(\mytele_A)}{\mytmn_1}{\myhead(\mytele_B)} \myand \cdots \myand \\
2835       \myind{2} \myjm{\mytmm_n}{\mytya_n}{\mytmn_n}{\mytyb_n}
2836     \end{array}
2837     $}
2838   \DisplayProof
2839
2840   \myderivsp
2841
2842   \AxiomC{$\myisreco(\mytyc{D}, \myctx)$}
2843   \UnaryInfC{$\myctx \vdash \myjm{\mytmm}{\mytyc{D} \myappsp \vec{A}}{\mytmn}{\mytyc{D} \myappsp \vec{B}} \myred foo$}
2844   \DisplayProof
2845   
2846   \myderivsp
2847   \AxiomC{}
2848   \UnaryInfC{$\mytya  \myeq  \mytyb  \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
2849   \DisplayProof
2850 }
2851
2852 \subsubsection{$\myprop$ and the hierarchy}
2853
2854 Where is $\myprop$ placed in the $\mytyp$ hierarchy?  At each universe
2855 level, we will have that 
2856
2857 \subsubsection{Quotation and irrelevance}
2858 \ref{sec:kant-irr}
2859
2860 foo
2861
2862 \section{\mykant : The practice}
2863 \label{sec:kant-practice}
2864
2865 The codebase consists of around 2500 lines of Haskell, as reported by
2866 the \texttt{cloc} utility.  The high level design is inspired by Conor
2867 McBride's work on various incarnations of Epigram, and specifically by
2868 the first version as described \citep{McBride2004} and the codebase for
2869 the new version \footnote{Available intermittently as a \texttt{darcs}
2870   repository at \url{http://sneezy.cs.nott.ac.uk/darcs/Pig09}.}.  In
2871 many ways \mykant\ is something in between the first and second version
2872 of Epigram.
2873
2874 The interaction happens in a read-eval-print loop (REPL).  The REPL is a
2875 available both as a commandline application and in a web interface,
2876 which is available at \url{kant.mazzo.li} and presents itself as in
2877 figure \ref{fig:kant-web}.
2878
2879 \begin{figure}
2880   \centering{
2881     \includegraphics[scale=1.0]{kant-web.png}
2882   }
2883   \caption{The \mykant\ web prompt.}
2884   \label{fig:kant-web}
2885 \end{figure}
2886
2887 The interaction with the user takes place in a loop living in and updating a
2888 context \mykant\ declarations.  The user inputs a new declaration that goes
2889 through various stages starts with the user inputing a \mykant\ declaration or
2890 another REPL command, which then goes through various stages that can end up
2891 in a context update, or in failures of various kind.  The process is described
2892 diagrammatically in figure \ref{fig:kant-process}:
2893
2894 \begin{description}
2895 \item[Parse] In this phase the text input gets converted to a sugared
2896   version of the core language.
2897
2898 \item[Desugar] The sugared declaration is converted to a core term.
2899
2900 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
2901   which is necessary to implement the type hierarchy check.
2902
2903 \item[Elaborate] Convert the declaration to some context item, which might be
2904   a value declaration (type and body) or a data type declaration (constructors
2905   and destructors).  This phase works in tandem with \textbf{Typechecking},
2906   which in turns needs to \textbf{Evaluate} terms.
2907
2908 \item[Distill] and report the result.  `Distilling' refers to the process of
2909   converting a core term back to a sugared version that the user can
2910   visualise.  This can be necessary both to display errors including terms or
2911   to display result of evaluations or type checking that the user has
2912   requested.
2913
2914 \item[Pretty print] Format the terms in a nice way, and display the result to
2915   the user.
2916
2917 \end{description}
2918
2919 \begin{figure}
2920   \centering{\small
2921     \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
2922     corners, minimum height=2.5em, node distance=0.7cm]
2923       
2924       \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
2925       centered, inner sep=0pt, node distance=0.7cm]
2926       
2927       \tikzstyle{line} = [draw, -latex']
2928       
2929       \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
2930       centered, node distance=1.5cm]
2931       
2932       
2933       \begin{tikzpicture}[auto]
2934         \node [cloud] (user) {User};
2935         \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
2936         \node [block, below=of parse] (desugar) {Desugar};
2937         \node [block, below=of desugar] (reference) {Reference};
2938         \node [block, below=of reference] (elaborate) {Elaborate};
2939         \node [block, left=of elaborate] (tycheck) {Typecheck};
2940         \node [block, left=of tycheck] (evaluate) {Evaluate};
2941         \node [decision, right=of elaborate] (error) {Error?};
2942         \node [block, right=of parse] (distill) {Distill};
2943         \node [block, right=of desugar] (update) {Update context};
2944         
2945         \path [line] (user) -- (parse);
2946         \path [line] (parse) -- (desugar);
2947         \path [line] (desugar) -- (reference);
2948         \path [line] (reference) -- (elaborate);
2949         \path [line] (elaborate) edge[bend right] (tycheck);
2950         \path [line] (tycheck) edge[bend right] (elaborate);
2951         \path [line] (elaborate) -- (error);
2952         \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
2953         \path [line] (error) -- node [near start] {no} (update);
2954         \path [line] (update) -- (distill);
2955         \path [line] (distill) -- (user);
2956         \path [line] (tycheck) edge[bend right] (evaluate);
2957         \path [line] (evaluate) edge[bend right] (tycheck);
2958       \end{tikzpicture}
2959   }
2960   \caption{High level overview of the life of a \mykant\ prompt cycle.}
2961   \label{fig:kant-process}
2962 \end{figure}
2963
2964 \subsection{Parsing and Sugar}
2965
2966 \subsection{Term representation and context}
2967 \label{sec:term-repr}
2968
2969 \subsection{Type checking}
2970
2971 \subsection{Type hierarchy}
2972 \label{sec:hier-impl}
2973
2974 \subsection{Elaboration}
2975
2976 \section{Evaluation}
2977
2978 \section{Future work}
2979
2980 % TODO coinduction (obscoin, gimenez), pattern unification (miller,
2981 % gundry), partiality monad (NAD)
2982
2983 \appendix
2984
2985 \section{Notation and syntax}
2986
2987 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
2988 the type of relation being established and the syntactic elements appearing,
2989 for example
2990
2991 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
2992   Typing derivations here.
2993 }
2994
2995 In the languages presented and Agda code samples I also highlight the syntax,
2996 following a uniform color and font convention:
2997
2998 \begin{center}
2999   \begin{tabular}{c | l}
3000     $\mytyc{Sans}$   & Type constructors. \\
3001     $\mydc{sans}$    & Data constructors. \\
3002     % $\myfld{sans}$  & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
3003     $\mysyn{roman}$  & Keywords of the language. \\
3004     $\myfun{roman}$  & Defined values and destructors. \\
3005     $\myb{math}$     & Bound variables.
3006   \end{tabular}
3007 \end{center}
3008
3009 Moreover, I will from time to time give examples in the Haskell programming
3010 language as defined in \citep{Haskell2010}, which I will typeset in
3011 \texttt{teletype} font.  I assume that the reader is already familiar with
3012 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
3013
3014 When presenting grammars, I will use a word in $\mysynel{math}$ font
3015 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate nonterminals. Additionally,
3016 I will use quite flexibly a $\mysynel{math}$ font to indicate a syntactic
3017 element.  More specifically, terms are usually indicated by lowercase letters
3018 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase letter (often
3019 $\mytya$, $\mytyb$, or $\mytycc$).
3020
3021 When presenting type derivations, I will often abbreviate and present multiple
3022 conclusions, each on a separate line:
3023 \begin{prooftree}
3024   \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
3025   \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
3026   \noLine
3027   \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
3028 \end{prooftree}
3029
3030 I will often present `definition' in the described calculi and in
3031 $\mykant$\ itself, like so:
3032 {\small\[
3033 \begin{array}{@{}l}
3034   \myfun{name} : \mytysyn \\
3035   \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
3036 \end{array}
3037 \]}
3038 To define operators, I use a mixfix notation similar
3039 to Agda, where $\myarg$s denote arguments, for example
3040 {\small\[
3041 \begin{array}{@{}l}
3042   \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
3043   \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
3044 \end{array}
3045 \]}
3046
3047 \section{Code}
3048
3049 \subsection{ITT renditions}
3050 \label{app:itt-code}
3051
3052 \subsubsection{Agda}
3053 \label{app:agda-itt}
3054
3055 Note that in what follows rules for `base' types are
3056 universe-polymorphic, to reflect the exposition.  Derived definitions,
3057 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
3058 that in the theory presented we don't have universe polymorphism.
3059
3060 \begin{code}
3061 module ITT where
3062   open import Level
3063
3064   data Empty : Set where
3065
3066   absurd : ∀ {a} {A : Set a} → Empty → A
3067   absurd ()
3068
3069   ¬_ : ∀ {a} → (A : Set a) → Set a
3070   ¬ A = A → Empty
3071
3072   record Unit : Set where
3073     constructor tt
3074
3075   record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
3076     constructor _,_
3077     field
3078       fst  : A
3079       snd  : B fst
3080   open _×_ public
3081
3082   data Bool : Set where
3083     true false : Bool
3084
3085   if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
3086   if true / _ then x else _ = x
3087   if false / _ then _ else x = x
3088
3089   if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
3090   if_then_else_ x {P} = if_/_then_else_ x P
3091
3092   data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
3093     _◁_ : (s : S) → (P s → W S P) → W S P
3094
3095   rec : ∀ {a b} {S : Set a} {P : S → Set b}
3096     (C : W S P → Set) →       -- some conclusion we hope holds
3097     ((s : S) →                -- given a shape...
3098      (f : P s → W S P) →      -- ...and a bunch of kids...
3099      ((p : P s) → C (f p)) →  -- ...and C for each kid in the bunch...
3100      C (s ◁ f)) →             -- ...does C hold for the node?
3101     (x : W S P) →             -- If so, ...
3102     C x                       -- ...C always holds.
3103   rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
3104
3105 module Examples-→ where
3106   open ITT
3107
3108   data ℕ : Set where
3109     zero : ℕ
3110     suc : ℕ → ℕ
3111
3112   -- These pragmas are needed so we can use number literals.
3113   {-# BUILTIN NATURAL ℕ #-}
3114   {-# BUILTIN ZERO zero #-}
3115   {-# BUILTIN SUC suc #-}
3116
3117   data List (A : Set) : Set where
3118     [] : List A
3119     _∷_ : A → List A → List A
3120
3121   length : ∀ {A} → List A → ℕ
3122   length [] = zero
3123   length (_ ∷ l) = suc (length l)
3124
3125   _>_ : ℕ → ℕ → Set
3126   zero > _ = Empty
3127   suc _ > zero = Unit
3128   suc x > suc y = x > y
3129
3130   head : ∀ {A} → (l : List A) → length l > 0 → A
3131   head [] p = absurd p
3132   head (x ∷ _) _ = x
3133
3134 module Examples-× where
3135   open ITT
3136   open Examples-→
3137
3138   even : ℕ → Set
3139   even zero = Unit
3140   even (suc zero) = Empty
3141   even (suc (suc n)) = even n
3142
3143   6-even : even 6
3144   6-even = tt
3145
3146   5-not-even : ¬ (even 5)
3147   5-not-even = absurd
3148   
3149   there-is-an-even-number : ℕ × even
3150   there-is-an-even-number = 6 , 6-even
3151
3152   _∨_ : (A B : Set) → Set
3153   A ∨ B = Bool × (λ b → if b then A else B)
3154
3155   left : ∀ {A B} → A → A ∨ B
3156   left x = true , x
3157
3158   right : ∀ {A B} → B → A ∨ B
3159   right x = false , x
3160
3161   [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
3162   [ f , g ] x =
3163     (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
3164
3165 module Examples-W where
3166   open ITT
3167   open Examples-×
3168
3169   Tr : Bool → Set
3170   Tr b = if b then Unit else Empty
3171
3172   ℕ : Set
3173   ℕ = W Bool Tr
3174
3175   zero : ℕ
3176   zero = false ◁ absurd
3177
3178   suc : ℕ → ℕ
3179   suc n = true ◁ (λ _ → n)
3180
3181   plus : ℕ → ℕ → ℕ
3182   plus x y = rec
3183     (λ _ → ℕ)
3184     (λ b →
3185       if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
3186       then (λ _ f → (suc (f tt))) else (λ _ _ → y))
3187     x
3188
3189   List : (A : Set) → Set
3190   List A = W (A ∨ Unit) (λ s → Tr (fst s))
3191
3192   [] : ∀ {A} → List A
3193   [] = (false , tt) ◁ absurd
3194
3195   _∷_ : ∀ {A} → A → List A → List A
3196   x ∷ l = (true , x) ◁ (λ _ → l)
3197
3198   _++_ : ∀ {A} → List A → List A → List A
3199   l₁ ++ l₂ = rec
3200     (λ _ → List _ → List _)
3201     (λ s f c l → {!!})
3202     l₁ l₂
3203
3204 module Equality where
3205   open ITT
3206   
3207   data _≡_ {a} {A : Set a} : A → A → Set a where
3208     refl : ∀ x → x ≡ x
3209
3210   ≡-elim : ∀ {a b} {A : Set a}
3211     (P : (x y : A) → x ≡ y → Set b) →
3212     ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
3213   ≡-elim P p (refl x) = p
3214
3215   subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
3216   subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
3217
3218   sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
3219   sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
3220
3221   trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
3222   trans x y z p q = subst (λ z′ → x ≡ z′) q p
3223
3224   cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y 
3225   cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
3226 \end{code}
3227
3228 \subsubsection{\mykant}
3229
3230 The following things are missing: $\mytyc{W}$-types, since our
3231 positivity check is overly strict, and equality, since we haven't
3232 implemented that yet.
3233
3234 {\small
3235 \verbatiminput{itt.ka}
3236 }
3237
3238 \subsection{\mykant\ examples}
3239
3240 {\small
3241 \verbatiminput{examples.ka}
3242 }
3243
3244 \subsection{\mykant's hierachy}
3245
3246 This rendition of the Hurken's paradox does not type check with the
3247 hierachy enabled, type checks and loops without it.  Adapted from an
3248 Agda version, available at
3249 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
3250
3251 {\small
3252 \verbatiminput{hurkens.ka}
3253 }
3254
3255 \bibliographystyle{authordate1}
3256 \bibliography{thesis}
3257
3258 \end{document}