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