sigh
[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 %% diagrams
36 \usepackage{tikz}
37 \usetikzlibrary{shapes,arrows,positioning}
38 % \usepackage{tikz-cd}
39 % \usepackage{pgfplots}
40
41
42 %% -----------------------------------------------------------------------------
43 %% Commands for Agda
44 \usepackage[english]{babel}
45 \usepackage[conor]{agda}
46 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
47 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
48 \renewcommand{\AgdaField}{\AgdaFunction}
49 % \definecolor{AgdaBound} {HTML}{000000}
50 \definecolor{AgdaHole} {HTML} {FFFF33}
51
52 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
53 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
54 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
55 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
56 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
57 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
58
59
60 %% -----------------------------------------------------------------------------
61 %% Commands
62
63 \newcommand{\mysyn}{\AgdaKeyword}
64 \newcommand{\mytyc}{\AgdaDatatype}
65 \newcommand{\mydc}{\AgdaInductiveConstructor}
66 \newcommand{\myfld}{\AgdaField}
67 \newcommand{\myfun}{\AgdaFunction}
68 % TODO make this use AgdaBound
69 \newcommand{\myb}[1]{\AgdaBound{#1}}
70 \newcommand{\myfield}{\AgdaField}
71 \newcommand{\myind}{\AgdaIndent}
72 \newcommand{\mykant}{\textsc{Kant}}
73 \newcommand{\mysynel}[1]{#1}
74 \newcommand{\myse}{\mysynel}
75 \newcommand{\mytmsyn}{\mysynel{term}}
76 \newcommand{\mysp}{\ }
77 % TODO \mathbin or \mathre here?
78 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
79 \newcommand{\myappsp}{\hspace{0.07cm}}
80 \newcommand{\myapp}[2]{#1 \myappsp #2}
81 \newcommand{\mysynsep}{\ \ |\ \ }
82
83 \FrameSep0.2cm
84 \newcommand{\mydesc}[3]{
85   \noindent
86   \mbox{
87     \parbox{\textwidth}{
88       {\small
89         \vspace{0.3cm}
90         \hfill \textbf{#1} $#2$
91
92         \framebox[\textwidth]{
93           \parbox{\textwidth}{
94             \vspace{0.1cm}
95             #3
96             \vspace{0.1cm}
97           }
98         }
99       }
100     }
101   }
102 }
103
104 % TODO is \mathbin the correct thing for arrow and times?
105
106 \newcommand{\mytmt}{\mysynel{t}}
107 \newcommand{\mytmm}{\mysynel{m}}
108 \newcommand{\mytmn}{\mysynel{n}}
109 \newcommand{\myred}{\leadsto}
110 \newcommand{\mysub}[3]{#1[#2 / #3]}
111 \newcommand{\mytysyn}{\mysynel{type}}
112 \newcommand{\mybasetys}{K}
113 % TODO change this name
114 \newcommand{\mybasety}[1]{B_{#1}}
115 \newcommand{\mytya}{\myse{A}}
116 \newcommand{\mytyb}{\myse{B}}
117 \newcommand{\mytycc}{\myse{C}}
118 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
119 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
120 \newcommand{\myctx}{\Gamma}
121 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
122 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
123 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
124 % TODO \mathbin or \mathrel here?
125 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
126 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
127 \newcommand{\myunit}{\mytyc{Unit}}
128 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
129 \newcommand{\myfst}{\myfld{fst}}
130 \newcommand{\mysnd}{\myfld{snd}}
131 \newcommand{\myconst}{\myse{c}}
132 \newcommand{\myemptyctx}{\cdot}
133 \newcommand{\myhole}{\AgdaHole}
134 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
135 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
136 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
137 \newcommand{\myright}[1]{\mydc{right}_{#1}}
138 \newcommand{\myempty}{\mytyc{Empty}}
139 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
140 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
141 \newcommand{\myarg}{\_}
142 \newcommand{\myderivsp}{\vspace{0.3cm}}
143 \newcommand{\mytyp}{\mytyc{Type}}
144 \newcommand{\myneg}{\myfun{$\neg$}}
145 \newcommand{\myar}{\,}
146 \newcommand{\mybool}{\mytyc{Bool}}
147 \newcommand{\mytrue}{\mydc{true}}
148 \newcommand{\myfalse}{\mydc{false}}
149 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
150 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
151 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
152 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
153 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
154 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
155 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
156 \newcommand{\mylist}{\mytyc{List}}
157 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
158 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
159 \newcommand{\myfoldr}{\myfun{foldr}}
160 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
161 \newcommand{\mynode}[2]{\mathbin{\mydc{$\lhd$}_{#1.#2}}}
162 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
163 \newcommand{\mylub}{\sqcup}
164 \newcommand{\mydefeq}{\cong}
165 \newcommand{\myrefl}{\mydc{refl}}
166 \newcommand{\mypeq}[1]{\mathrel{\mytyc{=}_{#1}}}
167 \newcommand{\myjeqq}{\myfun{=-elim}}
168 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
169 \newcommand{\mysubst}{\myfun{subst}}
170 \newcommand{\myprsyn}{\myse{prop}}
171 \newcommand{\myprdec}[1]{\llbracket #1 \rrbracket}
172 \newcommand{\myand}{\wedge}
173 \newcommand{\myprfora}[3]{\forall #1 {:} #2. #3}
174 \newcommand{\mybot}{\bot}
175 \newcommand{\mytop}{\top}
176 \newcommand{\mycoe}{\myfun{coe}}
177 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
178 \newcommand{\mycoh}{\myfun{coh}}
179 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
180 \newcommand{\myjm}[4]{(#1 {:} #2) = (#3 {:} #4)}
181 \newcommand{\myprop}{\mytyc{Prop}}
182 \newcommand{\mytmup}{\mytmsyn\uparrow}
183 \newcommand{\mydefs}{\Delta}
184 \newcommand{\mynf}{\Downarrow}
185 \newcommand{\myinff}[3]{#1 \vdash #2 \Rightarrow #3}
186 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
187 \newcommand{\mychkk}[3]{#1 \vdash #2 \Leftarrow #3}
188 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
189 \newcommand{\myann}[2]{#1 : #2}
190 \newcommand{\mydeclsyn}{\myse{decl}}
191 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
192 \newcommand{\mypost}[2]{\mysyn{postulate}\ #1 : #2}
193 \newcommand{\myadt}[4]{\mysyn{data}\ #1 : #2 \myarr \mytyp\ \mysyn{where}\ #3\{ #4 \}}
194 \newcommand{\myreco}[4]{\mysyn{record}\ #1 : #2 \myarr \mytyp\ \mysyn{where}\ #3\ \{ #4 \}}
195 % TODO change vdash
196 \newcommand{\myelabt}{\vdash}
197 \newcommand{\myelabf}{\rhd}
198 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
199 \newcommand{\mytele}{\Delta}
200 \newcommand{\mytelee}{\delta}
201 \newcommand{\mydcctx}{\Gamma}
202 \newcommand{\mynamesyn}{\myse{name}}
203 \newcommand{\myvec}{\overrightarrow}
204 \newcommand{\mymeta}{\textsc}
205 \newcommand{\myhyps}{\mymeta{hyps}}
206 \newcommand{\mycc}{;}
207 \newcommand{\myemptytele}{\cdot}
208 \newcommand{\mymetagoes}{\Longrightarrow}
209 % \newcommand{\mytesctx}{\
210 \newcommand{\mytelesyn}{\myse{telescope}}
211 \newcommand{\myrecs}{\mymeta{recs}}
212
213 %% -----------------------------------------------------------------------------
214
215 \title{\mykant: Implementing Observational Equality}
216 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
217 \date{June 2013}
218
219 \begin{document}
220
221 \iffalse
222 \begin{code}
223 module thesis where
224 \end{code}
225 \fi
226
227 \maketitle
228
229 \begin{abstract}
230   The marriage between programming and logic has been a very fertile one.  In
231   particular, since the simply typed lambda calculus (STLC), a number of type
232   systems have been devised with increasing expressive power.
233
234   Section \ref{sec:types} will give a very brief overview of STLC, and then
235   illustrate how it can be interpreted as a natural deduction system.  Section
236   \ref{sec:itt} will introduce Inutitionistic Type Theory (ITT), which expands
237   on this concept, employing a more expressive logic.  The exposition is quite
238   dense since there is a lot of material to cover; for a more complete treatment
239   of the material the reader can refer to \citep{Thompson1991, Pierce2002}.
240   Section \ref{sec:equality} will explain why equality has always been a tricky
241   business in these theories, and talk about the various attempts that have been
242   made to make the situation better.  One interesting development has recently
243   emerged: Observational Type theory.
244
245   Section \ref{sec:practical} will describe common extensions found in the
246   systems currently in use.  Finally, section \ref{sec:kant} will describe a
247   system developed by the author that implements a core calculus based on the
248   principles described.
249 \end{abstract}
250
251 \clearpage
252
253 \tableofcontents
254
255 \clearpage
256
257 \section{Simple and not-so-simple types}
258 \label{sec:types}
259
260 \subsection{The untyped $\lambda$-calculus}
261
262 Along with Turing's machines, the earliest attempts to formalise computation
263 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
264 language encodes computation with a minimal syntax and no `data' in the
265 traditional sense, but just functions.  Here we give a brief overview of the
266 language, which will give the chance to introduce concepts central to the
267 analysis of all the following calculi.  The exposition follows the one found in
268 chapter 5 of \cite{Queinnec2003}.
269
270 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
271 and applications:
272
273 \mydesc{syntax}{ }{
274   $
275   \begin{array}{r@{\ }c@{\ }l}
276     \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
277     x          & \in & \text{Some enumerable set of symbols}
278   \end{array}
279   $
280 }
281
282 Parenthesis will be omitted in the usual way:
283 $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
284 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
285
286 Abstractions roughly corresponds to functions, and their semantics is more
287 formally explained by the $\beta$-reduction rule:
288
289 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
290   $
291   \begin{array}{l}
292     \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{, where} \\
293     \myind{1}
294     \begin{array}{l@{\ }c@{\ }l}
295       \mysub{\myb{x}}{\myb{x}}{\mytmn} & = & \mytmn \\
296       \mysub{\myb{y}}{\myb{x}}{\mytmn} & = & y\text{, with } \myb{x} \neq y \\
297       \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & = & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
298       \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{x}}{\mytmm} \\
299       \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}}, \\
300       \multicolumn{3}{l}{\myind{1} \text{with $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
301     \end{array}
302   \end{array}
303   $
304 }
305
306 The care required during substituting variables for terms is required to avoid
307 name capturing.  We will use substitution in the future for other name-binding
308 constructs assuming similar precautions.
309
310 These few elements are of remarkable expressiveness, and in fact Turing
311 complete.  As a corollary, we must be able to devise a term that reduces forever
312 (`loops' in imperative terms):
313 \[
314   (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
315 \]
316
317 A \emph{redex} is a term that can be reduced.  In the untyped $\lambda$-calculus
318 this will be the case for an application in which the first term is an
319 abstraction, but in general we call aterm reducible if it appears to the left of
320 a reduction rule.  When a term contains no redexes it's said to be in
321 \emph{normal form}.  Given the observation above, not all terms reduce to a
322 normal forms: we call the ones that do \emph{normalising}, and the ones that
323 don't \emph{non-normalising}.
324
325 The reduction rule presented is not syntax directed, but \emph{evaluation
326   strategies} can be employed to reduce term systematically. Common evaluation
327 strategies include \emph{call by value} (or \emph{strict}), where arguments of
328 abstractions are reduced before being applied to the abstraction; and conversely
329 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
330 to proceed---in other words when we have an application where the function is
331 still not a $\lambda$. In both these reduction strategies we never reduce under
332 an abstraction: for this reason a weaker form of normalisation is used, where
333 both abstractions and normal forms are said to be in \emph{weak head normal
334   form}.
335
336 \subsection{The simply typed $\lambda$-calculus}
337
338 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
339 \emph{types} to them, and then check that the terms that we are forming make
340 sense given our typing rules \citep{Curry1934}.  The first most basic instance
341 of this idea takes the name of \emph{simply typed $\lambda$ calculus}, whose
342 rules are shown in figure \ref{fig:stlc}.
343
344 Our types contain a set of \emph{type variables} $\Phi$, which might correspond
345 to some `primitive' types; and $\myarr$, the type former for `arrow' types, the
346 types of functions.  The language is explicitly typed: when we bring a variable
347 into scope with an abstraction, we explicitly declare its type. $\mytya$,
348 $\mytyb$, $\mytycc$, will be used to refer to a generic type.  Reduction is
349 unchanged from the untyped $\lambda$-calculus.
350
351 \begin{figure}[t]
352   \mydesc{syntax}{ }{
353     $
354     \begin{array}{r@{\ }c@{\ }l}
355       \mytmsyn   & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
356       (\myapp{\mytmsyn}{\mytmsyn}) \\
357       \mytysyn   & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn  \mysynsep \\
358       \myb{x}    & \in & \text{Some enumerable set of symbols} \\
359       \myse{\phi} & \in & \Phi
360     \end{array}
361     $
362   }
363   
364   \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
365     \centering{
366       \begin{tabular}{ccc}
367         \AxiomC{$\myctx(x) = A$}
368         \UnaryInfC{$\myjud{\myb{x}}{A}$}
369         \DisplayProof
370         &
371         \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
372         \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
373         \DisplayProof
374         &
375         \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
376         \AxiomC{$\myjud{\mytmn}{\mytya}$}
377         \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
378         \DisplayProof
379       \end{tabular}
380     }
381 }
382   \caption{Syntax and typing rules for the STLC.  Reduction is unchanged from
383     the untyped $\lambda$-calculus.}
384   \label{fig:stlc}
385 \end{figure}
386
387 In the typing rules, a context $\myctx$ is used to store the types of bound
388 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
389 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
390
391 This typing system takes the name of `simply typed lambda calculus' (STLC), and
392 enjoys a number of properties.  Two of them are expected in most type systems
393 \citep{Pierce2002}:
394 \begin{description}
395 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
396   constructor does not appear on the left of the $\myred$ relation (currently
397   only $\lambda$), or it can take a step according to the evaluation rules.
398 \item[Preservation] If a well-typed term takes a step of evaluation, then the
399   resulting term is also well-typed, and preserves the previous type.  Also
400   known as \emph{subject reduction}.
401 \end{description}
402
403 However, STLC buys us much more: every well-typed term is normalising
404 \citep{Tait1967}.  It is easy to see that we can't fill the blanks if we want to
405 give types to the non-normalising term shown before:
406 \begin{equation*}
407   \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
408 \end{equation*}
409
410 This makes the STLC Turing incomplete.  We can recover the ability to loop by
411 adding a combinator that recurses:
412
413 \noindent
414 \begin{minipage}{0.5\textwidth}
415 \mydesc{syntax}{ } {
416   $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
417   \vspace{0.4cm}
418 }
419 \end{minipage} 
420 \begin{minipage}{0.5\textwidth}
421 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
422   \centering{
423     \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
424     \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
425     \DisplayProof
426   }
427 }
428 \end{minipage} 
429
430 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
431   \centering{
432     $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
433   }
434 }
435
436 This will deprive us of normalisation, which is a particularly bad thing if we
437 want to use the STLC as described in the next section.
438
439 \subsection{The Curry-Howard correspondence}
440
441 It turns out that the STLC can be seen a natural deduction system for
442 intuitionistic propositional logic.  Terms are proofs, and their types are the
443 propositions they prove.  This remarkable fact is known as the Curry-Howard
444 correspondence, or isomorphism.
445
446 The arrow ($\myarr$) type corresponds to implication.  If we wish to prove that
447 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
448 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
449 correct type:
450 \[
451   \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
452 \]
453 That is, function composition.  Going beyond arrow types, we can extend our bare
454 lambda calculus with useful types to represent other logical constructs, as
455 shown in figure \ref{fig:natded}.
456
457 \begin{figure}[t]
458 \mydesc{syntax}{ }{
459   $
460   \begin{array}{r@{\ }c@{\ }l}
461     \mytmsyn & ::= & \cdots \\
462              &  |  & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
463              &  |  & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
464                      \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
465                      \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
466              &  |  & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
467                      \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
468     \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
469   \end{array}
470   $
471 }
472
473 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
474   \centering{
475     \begin{tabular}{cc}
476       $
477       \begin{array}{l@{ }l@{\ }c@{\ }l}
478         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
479           \myapp{\mytmm}{\mytmt} \\
480         \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
481           \myapp{\mytmn}{\mytmt}
482       \end{array}
483       $
484       &
485       $
486       \begin{array}{l@{ }l@{\ }c@{\ }l}
487         \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
488         \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
489       \end{array}
490       $
491     \end{tabular}
492   }
493 }
494
495 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
496   \centering{
497     \begin{tabular}{cc}
498       \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
499       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
500       \DisplayProof
501       &
502       \AxiomC{$\myjud{\mytmt}{\myempty}$}
503       \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
504       \DisplayProof
505     \end{tabular}
506   }
507   \myderivsp
508   \centering{
509     \begin{tabular}{cc}
510       \AxiomC{$\myjud{\mytmt}{\mytya}$}
511       \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
512       \DisplayProof
513       &
514       \AxiomC{$\myjud{\mytmt}{\mytyb}$}
515       \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
516       \DisplayProof
517
518     \end{tabular}
519   }
520   \myderivsp
521   \centering{
522     \begin{tabular}{cc}
523       \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
524       \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
525       \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
526       \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
527       \DisplayProof
528     \end{tabular}
529   }
530   \myderivsp
531   \centering{
532     \begin{tabular}{ccc}
533       \AxiomC{$\myjud{\mytmm}{\mytya}$}
534       \AxiomC{$\myjud{\mytmn}{\mytyb}$}
535       \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
536       \DisplayProof
537       &
538       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
539       \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
540       \DisplayProof
541       &
542       \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
543       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
544       \DisplayProof
545     \end{tabular}
546   }
547 }
548 \caption{Rules for the extendend STLC.  Only the new features are shown, all the
549   rules and syntax for the STLC apply here too.}
550   \label{fig:natded}
551 \end{figure}
552
553 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either} in
554 Haskell) correspond to disjunctions, and dually tuples (or pairs, or
555 products---$\myprod$ here, tuples in Haskell) correspond to conjunctions.  This
556 is apparent looking at the ways to construct and destruct the values inhabiting
557 those types: for $\mysum$ $\myleft{ }$ and $\myright{ }$ correspond to $\vee$
558 introduction, and $\mycase{\_}{\_}$ to $\vee$ elimination; for $\myprod$
559 $\mypair{\_}{\_}$ corresponds to $\wedge$ introduction, $\myfst$ and $\mysnd$ to
560 $\wedge$ elimination.
561
562 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
563 $\myempty$ corresponds to the logical $\bot$.  $\myunit$ has one introduction
564 rule ($\mytt$), and thus one inhabitant; and no eliminators.  $\myempty$ has no
565 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
566 }$), corresponding to the logical \emph{ex falso quodlibet}.  Note that in the
567 constructors for the sums and the destructor for $\myempty$ we need to include
568 some type information to keep type checking decidable.
569
570 With these rules, our STLC now looks remarkably similar in power and use to the
571 natural deduction we already know.  $\myneg \mytya$ can be expressed as $\mytya
572 \myarr \myempty$.  However, there is an important omission: there is no term of
573 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
574 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
575 a type equivalent to those.
576
577 This has a considerable effect on our logic and it's no coincidence, since there
578 is no obvious computational behaviour for laws like the excluded middle.
579 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
580 and all the systems analysed will have this characteristic since they build on
581 the foundation of the STLC\footnote{There is research to give computational
582   behaviour to classical logic, but I will not touch those subjects.}.
583
584 As in logic, if we want to keep our system consistent, we must make sure that no
585 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
586 The variant of STLC presented here is indeed
587 consistent, a result that follows from the fact that it is
588 normalising. % TODO explain
589 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
590 desire for consistency.  The following term works for every type $\mytya$,
591 including bottom:
592 \[
593 (\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya
594 \]
595
596 \subsection{Inductive data}
597
598 To make the STLC more useful as a programming language or reasoning tool it is
599 common to include (or let the user define) inductive data types.  These comprise
600 of a type former, various constructors, and an eliminator (or destructor) that
601 serves as primitive recursor.
602
603 For example, we might add a $\mylist$ type constructor, along with an `empty
604 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor.  The eliminator for
605 lists will be the usual folding operation ($\myfoldr$).  See figure
606 \ref{fig:list}.
607
608 \begin{figure}[h]
609 \mydesc{syntax}{ }{
610   $
611   \begin{array}{r@{\ }c@{\ }l}
612     \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
613                      \mysynsep
614                      \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
615     \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
616   \end{array}
617   $
618 }
619 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
620   \centering{
621   $
622   \begin{array}{l@{\ }c@{\ }l}
623     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
624
625     \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
626     \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
627   \end{array}
628   $
629 }
630 }
631 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
632   \centering{
633     \begin{tabular}{cc}
634       \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
635       \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
636       \DisplayProof
637       &
638       \AxiomC{$\myjud{\mytmm}{\mytya}$}
639       \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
640       \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
641       \DisplayProof
642     \end{tabular}
643   }
644   \myderivsp
645   \centering{
646     \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
647     \AxiomC{$\myjud{\mytmm}{\mytyb}$}
648     \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
649     \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
650     \DisplayProof
651   }
652 }
653 \caption{Rules for lists in the STLC.}
654 \label{fig:list}
655 \end{figure}
656
657 In section \ref{sec:well-order} we will see how to give a general account of
658 inductive data.  %TODO does this make sense to have here?
659
660 \section{Intuitionistic Type Theory}
661 \label{sec:itt}
662
663 \subsection{Extending the STLC}
664
665 The STLC can be made more expressive in various ways.  \cite{Barendregt1991}
666 succinctly expressed geometrically how we can add expressivity:
667
668 $$
669 \xymatrix@!0@=1.5cm{
670   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
671   & & \lambda C \ar@{-}[dd]
672   \\
673   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
674   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
675   \\
676   & \lambda\underline\omega \ar@{-}'[r][rr]
677   & & \lambda P\underline\omega
678   \\
679   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
680   & & \lambda P \ar@{-}[ur]
681 }
682 $$
683 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
684 3 dimensions:
685 \begin{description}
686 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
687   types in our type signatures.  For example, we can define a polymorphic
688   identity function:
689   \[\displaystyle
690   (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} : \mytyp) \myarr \myb{A} \myarr \myb{A}
691   \]
692   The first and most famous instance of this idea has been System F.  This form
693   of polymorphism and has been wildly successful, also thanks to a well known
694   inference algorithm for a restricted version of System F known as
695   Hindley-Milner.  Languages like Haskell and SML are based on this discipline.
696 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
697   type operators.  For example we could define a function that given types $R$
698   and $\mytya$ forms the type that represents a value of type $\mytya$ in
699   continuation passing style: \[\displaystyle(\myabss{\myb{A} \myar \myb{R}}{\mytyp}{(\myb{A}
700     \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp\]
701 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
702   types', give great expressive power.  For example, we can have values of whose
703   type depend on a boolean:
704   \[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
705   \myarr \mytyp\]
706 \end{description}
707
708 All the systems preserve the properties that make the STLC well behaved.  The
709 system we are going to focus on, Intuitionistic Type Theory, has all of the
710 above additions, and thus would sit where $\lambda{C}$ sits in the
711 `$\lambda$-cube'.  It will serve as the logical `core' of all the other
712 extensions that we will present and ultimately our implementation of a similar
713 logic.
714
715 \subsection{A Bit of History}
716
717 Logic frameworks and programming languages based on type theory have a long
718 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
719 but then revised it since the original version was inconsistent due to its
720 impredicativity\footnote{In the early version there was only one universe
721   $\mytyp$ and $\mytyp : \mytyp$, see section \ref{sec:term-types} for an
722   explanation on why this causes problems.}.  For this reason he gave a revised
723 and consistent definition later \citep{Martin-Lof1984}.
724
725 A related development is the polymorphic $\lambda$-calculus, and specifically
726 the previously mentioned System F, which was developed independently by Girard
727 and Reynolds.  An overview can be found in \citep{Reynolds1994}.  The surprising
728 fact is that while System F is impredicative it is still consistent and strongly
729 normalising.  \cite{Coquand1986} further extended this line of work with the
730 Calculus of Constructions (CoC).
731
732 Most widely used interactive theorem provers are based on ITT.  Popular ones
733 include Agda \citep{Norell2007, Bove2009}, Coq \citep{Coq}, and Epigram
734 \citep{McBride2004, EpigramTut}.
735
736 \subsection{A note on inference}
737
738 % TODO do this, adding links to the sections about bidi type checking and
739 % implicit universes.
740 In the following text I will often omit explicit typing for abstractions or
741
742 Moreover, I will use $\mytyp$ without bothering to specify a
743 universe, with the silent assumption that the definition is consistent
744 regarding to the hierarchy.
745
746 \subsection{A simple type theory}
747 \label{sec:core-tt}
748
749 The calculus I present follows the exposition in \citep{Thompson1991}, and is
750 quite close to the original formulation of predicative ITT as found in
751 \citep{Martin-Lof1984}.  The system's syntax and reduction rules are presented
752 in their entirety in figure \ref{fig:core-tt-syn}.  The typing rules are
753 presented piece by piece.  An Agda rendition of the presented theory is
754 reproduced in appendix \ref{app:agda-code}.
755
756 \begin{figure}[t]
757 \mydesc{syntax}{ }{
758   $
759   \begin{array}{r@{\ }c@{\ }l}
760     \mytmsyn & ::= & \myb{x} \mysynsep
761                      \mytyp_{l} \mysynsep
762                      \myunit \mysynsep \mytt \mysynsep
763                      \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
764              &  |  & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
765                      \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
766              &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
767                      \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
768                      (\myapp{\mytmsyn}{\mytmsyn}) \\
769              &  |  & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
770                      \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
771              &  |  & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
772              &  |  & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
773                      \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
774              &  |  & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
775     l        & \in & \mathbb{N}
776   \end{array}
777   $
778 }
779
780 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
781   \centering{
782     \begin{tabular}{cc}
783       $
784       \begin{array}{l@{ }l@{\ }c@{\ }l}
785         \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
786         \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
787       \end{array}
788       $
789       &
790       $
791       \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
792       $
793       \myderivsp
794     \end{tabular}
795     $
796     \begin{array}{l@{ }l@{\ }c@{\ }l}
797       \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
798       \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
799     \end{array}
800     $
801     \myderivsp
802
803     $
804     \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
805     \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
806       \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
807     })}
808     $
809   }
810 }
811 \caption{Syntax and reduction rules for our type theory.}
812 \label{fig:core-tt-syn}
813 \end{figure}
814
815 \subsubsection{Types are terms, some terms are types}
816 \label{sec:term-types}
817
818 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
819   \centering{
820     \begin{tabular}{cc}
821       \AxiomC{$\myjud{\mytmt}{\mytya}$}
822       \AxiomC{$\mytya \mydefeq \mytyb$}
823       \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
824       \DisplayProof
825       &
826       \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
827       \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
828       \DisplayProof
829     \end{tabular}
830   }
831 }
832
833 The first thing to notice is that a barrier between values and types that we had
834 in the STLC is gone: values can appear in types, and the two are treated
835 uniformly in the syntax.
836
837 While the usefulness of doing this will become clear soon, a consequence is
838 that since types can be the result of computation, deciding type equality is
839 not immediate as in the STLC.  For this reason we define \emph{definitional
840   equality}, $\mydefeq$, as the congruence relation extending
841 $\myred$---moreover, when comparing types syntactically we do it up to
842 renaming of bound names ($\alpha$-renaming).  For example under this
843 discipline we will find that
844 \[
845 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}}
846 \]
847 Types that are definitionally equal can be used interchangeably.  Here the
848 `conversion' rule is not syntax directed, however we will see how it is
849 possible to employ $\myred$ to decide term equality in a systematic
850 way.  % TODO add section
851 Another thing to notice is that considering the need to reduce terms to decide
852 equality, it is essential for a dependently type system to be terminating and
853 confluent for type checking to be decidable.
854
855 Moreover, we specify a \emph{type hierarchy} to talk about `large' types:
856 $\mytyp_0$ will be the type of types inhabited by data: $\mybool$, $\mynat$,
857 $\mylist$, etc.  $\mytyp_1$ will be the type of $\mytyp_0$, and so on---for
858 example we have $\mytrue : \mybool : \mytyp_0 : \mytyp_1 : \cdots$.  Each type
859 `level' is often called a universe in the literature.  While it is possible,
860 to simplify things by having only one universe $\mytyp$ with $\mytyp :
861 \mytyp$, this plan is inconsistent for much the same reason that impredicative
862 na\"{\i}ve set theory is \citep{Hurkens1995}.  Moreover, various techniques
863 can be employed to lift the burden of explicitly handling universes.
864 % TODO add sectioon about universes
865
866 \subsubsection{Contexts}
867
868 \begin{minipage}{0.5\textwidth}
869   \mydesc{context validity:}{\myvalid{\myctx}}{
870     \centering{
871       \begin{tabular}{cc}
872         \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
873         \UnaryInfC{$\myvalid{\myemptyctx}$}
874         \DisplayProof
875         &
876         \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
877         \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
878         \DisplayProof
879       \end{tabular}
880     }
881   }
882 \end{minipage} 
883 \begin{minipage}{0.5\textwidth}
884   \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
885     \centering{
886       \AxiomC{$\myctx(x) = \mytya$}
887       \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
888       \DisplayProof
889     }
890   }
891 \end{minipage}
892 \vspace{0.1cm}
893
894 We need to refine the notion context to make sure that every variable appearing
895 is typed correctly, or that in other words each type appearing in the context is
896 indeed a type and not a value.  In every other rule, if no premises are present,
897 we assume the context in the conclusion to be valid.
898
899 Then we can re-introduce the old rule to get the type of a variable for a
900 context.
901
902 \subsubsection{$\myunit$, $\myempty$}
903
904 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
905   \centering{
906     \begin{tabular}{ccc}
907       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
908       \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
909       \noLine
910       \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
911       \DisplayProof
912       &
913       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
914       \UnaryInfC{$\myjud{\mytt}{\myunit}$}
915       \noLine
916       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
917       \DisplayProof
918       &
919       \AxiomC{$\myjud{\mytmt}{\myempty}$}
920       \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
921       \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
922       \noLine
923       \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
924       \DisplayProof
925     \end{tabular}
926   }
927 }
928
929 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
930 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
931 sure that we are invoking $\myabsurd{}$ over a type.
932
933 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
934
935 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
936  \centering{
937    \begin{tabular}{ccc}
938      \AxiomC{}
939      \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
940      \DisplayProof
941      &
942      \AxiomC{}
943      \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
944      \DisplayProof
945      &
946      \AxiomC{}
947       \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
948       \DisplayProof
949     \end{tabular}
950     \myderivsp
951
952     \AxiomC{$\myjud{\mytmt}{\mybool}$}
953     \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
954     \noLine
955     \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
956     \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
957     \DisplayProof
958
959   }
960 }
961
962 With booleans we get the first taste of `dependent' in `dependent types'.  While
963 the two introduction rules ($\mytrue$ and $\myfalse$) are not surprising, the
964 typing rules for $\myfun{if}$ are.  In most strongly typed languages we expect
965 the branches of an $\myfun{if}$ statements to be of the same type, to preserve
966 subject reduction, since execution could take both paths.  This is a pity, since
967 the type system does not reflect the fact that in each branch we gain knowledge
968 on the term we are branching on.  Which means that programs along the lines of
969 \begin{verbatim}
970 if null xs then head xs else 0
971 \end{verbatim}
972 are a necessary, well typed, danger.
973
974 However, in a more expressive system, we can do better: the branches' type can
975 depend on the value of the scrutinised boolean.  This is what the typing rule
976 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
977 representing the scrutinised boolean type, and the branches are typechecked with
978 the updated knowledge on the value of $\myb{x}$.
979
980 \subsubsection{$\myarr$, or dependent function}
981
982  \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
983    \centering{
984      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
985      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
986      \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
987      \DisplayProof
988
989      \myderivsp
990
991     \begin{tabular}{cc}
992       \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
993       \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
994       \DisplayProof
995       &
996       \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
997       \AxiomC{$\myjud{\mytmn}{\mytya}$}
998       \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
999       \DisplayProof
1000     \end{tabular}
1001   }
1002 }
1003
1004 Dependent functions are one of the two key features that perhaps most
1005 characterise dependent types---the other being dependent products.  With
1006 dependent functions, the result type can depend on the value of the argument.
1007 This feature, together with the fact that the result type might be a type
1008 itself, brings a lot of interesting possibilities.  Keeping the correspondence
1009 with logic alive, dependent functions are much like universal quantifiers
1010 ($\forall$) in logic.
1011
1012 Again, we need to make sure that the type hierarchy is respected, which is the
1013 reason why a type formed by $\myarr$ will live in the least upper bound of the
1014 levels of argument and return type.  This trend will continue with the other
1015 type-level binders, $\myprod$ and $\mytyc{W}$.
1016
1017 % TODO maybe examples?
1018
1019 \subsubsection{$\myprod$, or dependent product}
1020
1021
1022 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1023   \centering{
1024      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1025      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1026      \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1027      \DisplayProof
1028
1029      \myderivsp
1030
1031     \begin{tabular}{cc}
1032       \AxiomC{$\myjud{\mytmm}{\mytya}$}
1033       \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1034       \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1035       \noLine
1036       \UnaryInfC{\phantom{$--$}}
1037       \DisplayProof
1038       &
1039       \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1040       \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1041       \noLine
1042       \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1043       \DisplayProof
1044     \end{tabular}
1045
1046   }
1047 }
1048
1049
1050 \subsubsection{$\mytyc{W}$, or well-order}
1051 \label{sec:well-order}
1052
1053 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1054   \centering{
1055      \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1056      \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1057      \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1058      \DisplayProof
1059
1060      \myderivsp
1061
1062      \AxiomC{$\myjud{\mytmt}{\mytya}$}
1063      \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1064      \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1065      \DisplayProof
1066
1067      \myderivsp
1068
1069      \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1070      \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1071      \noLine
1072      \BinaryInfC{$\myjud{\myse{p}}{
1073        \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}}}}
1074      }$}
1075      \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1076      \DisplayProof
1077    }
1078 }
1079
1080 \section{The struggle for equality}
1081 \label{sec:equality}
1082
1083 In the previous section we saw how a type checker (or a human) needs a notion of
1084 \emph{definitional equality}.  Beyond this meta-theoretic notion, in this
1085 section we will explore the ways of expressing equality \emph{inside} the
1086 theory, as a reasoning tool available to the user.  This area is the main
1087 concern of this thesis, and in general a very active research topic, since we do
1088 not have a fully satisfactory solution, yet.
1089
1090 \subsection{Propositional equality}
1091
1092 \noindent
1093 \begin{minipage}{0.5\textwidth}
1094 \mydesc{syntax}{ }{
1095   $
1096   \begin{array}{r@{\ }c@{\ }l}
1097     \mytmsyn & ::= & \cdots \\
1098              &  |  & \mytmsyn \mypeq{\mytmsyn} \mytmsyn \mysynsep
1099                      \myapp{\myrefl}{\mytmsyn} \\
1100              &  |  & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1101   \end{array}
1102   $
1103 }
1104 \end{minipage} 
1105 \begin{minipage}{0.5\textwidth}
1106 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1107   \centering{
1108     $
1109     \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1110     $
1111   }
1112   \vspace{0.9cm}
1113 }
1114 \end{minipage}
1115
1116 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1117   \centering{
1118     \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1119     \AxiomC{$\myjud{\mytmm}{\mytya}$}
1120     \AxiomC{$\myjud{\mytmn}{\mytya}$}
1121     \TrinaryInfC{$\myjud{\mytmm \mypeq{\mytya} \mytmn}{\mytyp_l}$}
1122     \DisplayProof
1123
1124     \myderivsp
1125
1126     \begin{tabular}{cc}
1127       \AxiomC{\phantom{$\myctx P \mytyp_l$}}
1128       \noLine
1129       \UnaryInfC{$\myjud{\mytmt}{\mytya}$}
1130       \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\mytmt \mypeq{\mytya} \mytmt}$}
1131       \DisplayProof
1132       &
1133       \AxiomC{$\myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\myb{x} \mypeq{\mytya} \myb{y}}{\mytyp_l}}}$}
1134       \noLine
1135       \UnaryInfC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}\hspace{1.2cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}$}
1136       \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1137       \DisplayProof
1138     \end{tabular}
1139   }
1140 }
1141
1142 To express equality between two terms inside ITT, the obvious way to do so is
1143 to have the equality construction to be a type-former.  Here we present what
1144 has survived as the dominating form of equality in systems based on ITT up to
1145 the present day.
1146
1147 Our type former is $\mypeq{\mytya}$, which given a type (in this case
1148 $\mytya$) relates equal terms of that type.  $\mypeq{}$ has one introduction
1149 rule, $\myrefl$, which introduces an equality relation between definitionally
1150 equal terms---in the typing rule we display the term as `the same', meaning
1151 `the same up to $\mydefeq$'. % TODO maybe mention this earlier
1152
1153 Finally, we have one eliminator for $\mypeq{}$, $\myjeqq$.  $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1154 \begin{itemize}
1155 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1156   $\mytya$) and a proof of their equality
1157 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1158   $\myse{n}$) are equal
1159 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$, plus
1160   the trivial proof by reflexivity showing that $\myse{m}$ is equal to itself
1161 \end{itemize}
1162 Given these ingredients, $\myjeqq$ retuns a member of $\myse{P}$ applied to
1163 $\mytmm$, $\mytmn$, and $\myse{q}$.  In other words $\myjeqq$ takes a
1164 witness that $\myse{P}$ works with \emph{definitionally equal} terms, and
1165 returns a witness of $\myse{P}$ working with \emph{propositionally equal}
1166 terms.  Invokations of $\myjeqq$ will vanish when the equality proofs will
1167 reduce to invocations to reflexivity, at which point the arguments must be
1168 definitionally equal, and thus the provided
1169 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1170 can be returned.
1171
1172 While the $\myjeqq$ rule is slightly convoluted, ve can derive many more
1173 `friendly' rules from it, for example a more obvious `substitution' rule, that
1174 replaces equal for equal in predicates:
1175 \[
1176 \begin{array}{l}
1177 (\myabs{\myb{A}\ \myb{P}\ \myb{x}\ \myb{y}\ \myb{q}\ \myb{p}}{
1178   \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{q}}{\myb{p}}}) : \\
1179 \myind{1} \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}}}}}
1180 \end{array}
1181 \]
1182 This rule is often called $\myfun{subst}$---here we will invoke it without
1183 specifying the type ($\myb{A}$) and the sides of the equality
1184 ($\myb{x},\myb{y}$).
1185
1186 Once we have $\myfun{subst}$, we can easily prove more familiar laws regarding
1187 equality, such as symmetry, transitivity, and a congruence law:
1188
1189 % TODO finish this
1190
1191 \subsection{Common extensions}
1192
1193 eta law
1194
1195 congruence
1196
1197 UIP
1198
1199 \subsection{Limitations}
1200
1201 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1202   schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1203
1204 However, propositional equality as described is quite restricted when
1205 reasoning about equality beyond the term structure, which is what definitional
1206 equality gives us (extension notwithstanding).
1207
1208 The problem is best exemplified by \emph{function extensionality}.  In
1209 mathematics, we would expect to be able to treat functions that give equal
1210 output for equal input as the same.  When reasoning in a mechanised framework
1211 we ought to be able to do the same: in the end, without considering the
1212 operational behaviour, all functions equal extensionally are going to be
1213 replaceable with one another.
1214
1215 However this is not the case, or in other words with the tools we have we have
1216 no term of type
1217 \[
1218 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1219     \myb{A} \myarr \myb{B}}{
1220         (\myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{\myb{B}} \myapp{\myb{g}}{\myb{x}}}) \myarr
1221         \myb{f} \mypeq{\myb{A} \myarr \myb{B}} \myb{g}
1222     }
1223 }
1224 \]
1225 To see why this is the case, consider the functions
1226 \[\myabs{\myb{x}}{0 \mathrel{\myfun{+}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{+}} 0}\]
1227 where $\myfun{+}$ is defined by recursion on the first argument, gradually
1228 destructing it to build up successors of the second argument.  The two
1229 functions are clearly denotationally the same, and we can in fact prove that
1230 \[
1231 \myfora{\myb{x}}{\mynat}{(0 \mathrel{\myfun{+}} \myb{x}) \mypeq{\mynat} (\myb{x} \mathrel{\myfun{+}} 0)}
1232 \]
1233 By analysis on the $\myb{x}$.  However, the two functions are not
1234 definitionally equal, and thus we won't be able to get rid of the
1235 quantification.
1236
1237 For the reasons above, theories that offer a propositional equality similar to
1238 what we presented are called \emph{intensional}, as opposed to
1239 \emph{extensional}.  Most systems in wide use today (such as Agda, Coq and
1240 Epigram) are of this kind.
1241
1242 This is quite an annoyance that often makes reasoning awkward to execute.  It
1243 also extends to other fields, for example proving bisimulation between
1244 processes specified by coinduction, or in general proving equivalences based
1245 on the behaviour on a term.
1246
1247 \subsection{Equality reflection}
1248
1249 One way to `solve' this problem is by identifying propositional equality with
1250 definitional equality:
1251
1252 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1253   \centering{
1254     \AxiomC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}$}
1255     \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1256     \DisplayProof
1257   }
1258 }
1259
1260 This rule takes the name of \emph{equality reflection}, and is a very
1261 different rule from the ones we saw up to now: it links a typing judgement
1262 internal to the type theory to a meta-theoretic judgement that the type
1263 checker uses to work with terms.  It is easy to see the dangerous consequences
1264 that this causes:
1265 \begin{itemize}
1266 \item The rule is syntax directed, and the type checker is presumably expected
1267   to come up with equality proofs when needed.
1268 \item More worryingly, type checking becomes undecidable also because
1269   computing under false assumptions becomes unsafe.
1270   Consider for example
1271   \[
1272   \myabss{\myb{q}}{\mytya \mypeq{\mytyp} (\mytya \myarr \mytya)}{\myhole{?}}
1273   \]
1274   Using the assumed proof in tandem with equality reflection we could easily
1275   write a classic Y combinator, sending the compiler into a loop.
1276 \end{itemize}
1277
1278 Given these facts theories employing equality reflection, like NuPRL
1279 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1280 to keep the systems manageable.  % TODO more info, problems with that.
1281
1282 For all its faults, equality reflection does allow us to prove extensionality,
1283 using the extensions we gave above.  Assuming that $\myctx$ contains
1284 \[\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}}}\]
1285 We can then derive
1286 \begin{prooftree}
1287   \AxiomC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{q}}{\myb{x}}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}$}
1288   \RightLabel{equality reflection}
1289   \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1290   \RightLabel{congruence for $\lambda$s}
1291   \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1292   \RightLabel{$\eta$-law for $\lambda$}
1293   \UnaryInfC{$\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}$}
1294   \RightLabel{$\myrefl$}
1295   \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\myb{f} \mypeq{} \myb{g}}$}
1296 \end{prooftree}
1297
1298 Now, the question is: do we need to give up well-behavedness of our theory to
1299 gain extensionality?
1300
1301 \subsection{Observational equality}
1302 \label{sec:ott}
1303
1304 % TODO should we explain this in detail?
1305 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1306   Theory} (OTT), promises to keep the well behavedness of ITT while being able
1307 to gain many useful equality proofs\footnote{It is suspected that OTT gains
1308   \emph{all} the equality proofs of ETT, but no proof exists yet.}, including
1309 function extensionality.  The main idea is to give the user the possibility to
1310 \emph{coerce} (or transport) values from a type $\mytya$ to a type $\mytyb$,
1311 if the type checker can prove structurally that $\mytya$ and $\mytya$ are
1312 equal; and providing a value-level equality based on similar principles.  A
1313 brief overview is given below,
1314
1315 \mydesc{syntax}{ }{
1316   $
1317   \begin{array}{r@{\ }c@{\ }l}
1318     \mytmsyn & ::= & \cdots \\
1319              &  |  & \myprdec{\myprsyn} \mysynsep
1320                      \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
1321                      \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1322     \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1323                      \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn} \\\
1324              &  |  & \mytmsyn = \mytmsyn \mysynsep
1325                      \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
1326   \end{array}
1327   $
1328 }
1329 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1330   \centering{
1331   }
1332 }
1333
1334 The original presentation of OTT employs the theory presented above.  It is
1335 close to the one presented in section \ref{sec:itt}, with the additions
1336 presented above, and the change that only one the `first' universe, the type
1337 of small types ($\mytyp_0$), is present.
1338
1339 The propositional universe is meant to be where equality proofs live in.  The
1340 equality proofs are respectively between types ($\mytya = \mytyb$), and
1341 between values 
1342
1343
1344
1345 However, only one universe is present ($\mytyp_0$), and a \emph{propositional}
1346 universe is isolated, intended to be the universe where equality proofs live
1347 in.  Propositions (as long as our system is consistent) are inhabited only by
1348 one element, and thus can all be treated as definitionally equal.
1349
1350
1351
1352 % \section{Augmenting ITT}
1353 % \label{sec:practical}
1354
1355 % \subsection{A more liberal hierarchy}
1356
1357 % \subsection{Type inference}
1358
1359 % \subsubsection{Bidirectional type checking}
1360
1361 % \subsubsection{Pattern unification}
1362
1363 % \subsection{Pattern matching and explicit fixpoints}
1364
1365 % \subsection{Induction-recursion}
1366
1367 % \subsection{Coinduction}
1368
1369 % \subsection{Dealing with partiality}
1370
1371 % \subsection{Type holes}
1372
1373 \section{\mykant : the theory}
1374 \label{sec:kant-theory}
1375
1376 \mykant\ is an interactive theorem prover developed as part of this thesis.
1377 The plan is to present a core language which would be capable of serving as
1378 the basis for a more featureful system, while still presenting interesting
1379 features and more importantly observational equality.
1380
1381 The author learnt the hard way the implementations challenges for such a
1382 project, and while there is a solid and working base to work on, observational
1383 equality is not currently implemented.  However, a detailed plan on how to add
1384 it this functionality is provided, and should not prove to be too much work.
1385
1386 The features currently implemented in \mykant\ are:
1387
1388 \begin{description}
1389 \item[Full dependent types] As we would expect, we have dependent a system
1390   which is as expressive as the `best' corner in the lambda cube described in
1391   section \ref{sec:itt}.
1392
1393 \item[Implicit, cumulative universe hierarchy] The user does not need to
1394   specify universe level explicitly, and universes are \emph{cumulative}.
1395
1396 \item[User defined data types and records] Instead of forcing the user to
1397   choose from a restricted toolbox, we let her define inductive data types,
1398   with associated primitive recursion operators; or records, with associated
1399   projections for each field.
1400
1401 \item[Bidirectional type checking] While no `fancy' inference via unification
1402   is present, we take advantage of an type synthesis system in the style of
1403   \cite{Pierce2000}, extending the concept for user defined data types.
1404
1405 \item[Type holes] When building up programs interactively, it is useful to
1406   leave parts unfinished while exploring the current context.  This is what
1407   type holes are for.
1408 \end{description}
1409
1410 The planned features are:
1411
1412 \begin{description}
1413 \item[Observational equality] As described in section \ref{sec:ott} but
1414   extended to work with the type hierarchy and to admit equality between
1415   arbitrary data types.
1416
1417 \item[Coinductive data] ...
1418 \end{description}
1419
1420 We will analyse the features one by one, along with motivations and tradeoffs
1421 for the design decisions made.
1422
1423 \subsection{Bidirectional type checking}
1424
1425 We start by describing bidirectional type checking since it calls for fairly
1426 different typing rules that what we have seen up to now.  The idea is to have
1427 two kind of terms: terms for which a type can always be inferred, and terms
1428 that need to be checked against a type.  A nice observation is that this
1429 duality runs through the semantics of the terms: data destructors (function
1430 application, record projections, primitive re cursors) \emph{infer} types,
1431 while data constructors (abstractions, record/data types data constructors)
1432 need to be checked.  In the literature these terms are respectively known as
1433
1434 To introduce the concept and notation, we will revisit the STLC in a
1435 bidirectional style.  The presentation follows \cite{Loh2010}.
1436
1437 % TODO do this --- is it even necessary
1438
1439 % \subsubsection{Declarations and contexts}
1440
1441 % A \mykant declaration can be one of 4 kinds:
1442
1443 % \begin{description}
1444 % \item[Value] A declared variable, together with a type and a body.
1445 % \item[Postulate] An abstract variable, with a type but no body.
1446 % \item[Inductive data] A datatype, with a type constructor and various data
1447 %   constructors---somewhat similar to what we find in Haskell.  A primitive
1448 %   recursor (or `destructor') will be generated automatically.
1449 % \item[Record] A record, which consists of one data constructor and various
1450 %   fields, with no recursive occurrences.  We will explain the need for records
1451 %   later.
1452 % \end{description}
1453
1454 % The syntax of 
1455
1456 \subsection{Base terms and types}
1457
1458 Let us begin by describing the primitives available without the user defining
1459 any data types, and without equality.  The syntax given here is the one of the
1460 core (`desugared') terms, and the way we handle variables and substitution is
1461 left unspecified, and explained in section \ref{sec:term-repr}, along with
1462 other implementation issues.  We are also going to give an account of the
1463 implicit type hierarchy separately in section \ref{sec:term-hierarchy}, so as
1464 not to clutter derivation rules too much, and just treat types as
1465 impredicative for the time being.
1466
1467 \mydesc{syntax}{ }{
1468   $
1469   \begin{array}{r@{\ }c@{\ }l}
1470     \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
1471              &  |  & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1472                      \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1473                      (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
1474                      (\myann{\mytmsyn}{\mytmsyn}) \\
1475     \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
1476   \end{array}
1477   $
1478 }
1479
1480 The syntax for our calculus includes just two basic constructs: abstractions
1481 and $\mytyp$s.  Everything else will be provided by user-definable constructs.
1482 Since we let the user define values, we will need a context capable of
1483 carrying the body of variables along with their type.  We also want to make
1484 sure not to have duplicate top names, so we enforce that.
1485
1486 % \mytyc{D} \mysynsep \mytyc{D}.\mydc{c}
1487 %                        \mysynsep \mytyc{D}.\myfun{f} \mysynsep 
1488
1489 \mydesc{context validity:}{\myvalid{\myctx}}{
1490   \centering{
1491     \begin{tabular}{ccc}
1492       \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1493       \UnaryInfC{$\myvalid{\myemptyctx}$}
1494       \DisplayProof
1495       &
1496       \AxiomC{$\myjud{\mytya}{\mytyp}$}
1497       \AxiomC{$\mynamesyn \not\in \myctx$}
1498       \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
1499       \DisplayProof
1500       &
1501       \AxiomC{$\myjud{\mytmt}{\mytya}$}
1502       \AxiomC{$\myfun{f} \not\in \myctx$}
1503       \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
1504       \DisplayProof
1505     \end{tabular}
1506   }
1507 }
1508
1509 Now we can present the reduction rules, which are unsurprising.  We have the
1510 usual functional application ($\beta$-reduction), but also a rule to replace
1511 names with their bodies, if in the context ($\delta$-reduction), and one to
1512 discard type annotations.  For this reason the new reduction rules are
1513 dependent on the context:
1514
1515 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
1516   \centering{
1517     \begin{tabular}{ccc}
1518       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1519       \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
1520                   \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
1521       \DisplayProof
1522       &
1523       \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
1524       \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
1525       \DisplayProof
1526       &
1527       \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1528       \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
1529       \DisplayProof
1530     \end{tabular}
1531   }
1532 }
1533
1534 We want to define a \emph{weak head normal form} (WHNF) for our terms, to give
1535 a syntax directed presentation of type rules with no `conversion' rule.  We
1536 will consider all \emph{canonical} forms (abstractions and data constructors)
1537 to be in weak head normal form...  % TODO finish
1538
1539 We can now give types to our terms.  Using our definition of WHNF, I will use
1540 $\mytmm \mynf \mytmn$ to indicate that $\mytmm$'s normal form is $\mytmn$.
1541 This way, we can avoid the non syntax-directed conversion rule, giving a more
1542 algorithmic presentation of type checking.
1543
1544 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytysyn}{   
1545   \centering{
1546     \begin{tabular}{ccc}
1547       \AxiomC{$\myb{x} : A \in \myctx$ or $\myb{x} \mapsto \mytmt : A \in \myctx$}
1548       \UnaryInfC{$\myinf{\myb{x}}{A}$}
1549       \DisplayProof
1550       &
1551       \AxiomC{$\mychk{\mytmt}{\mytya}$}
1552       \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
1553       \DisplayProof
1554     \end{tabular}
1555     \myderivsp
1556
1557     \AxiomC{$\myinf{\mytmm}{\mytya}$}
1558     \AxiomC{$\myctx \vdash \mytya \mynf \myfora{\myb{x}}{\mytyb}{\myse{C}}$}
1559     \AxiomC{$\mychk{\mytmn}{\mytyb}$}
1560     \TrinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\myse{C}}{\myb{x}}{\mytmn}}$}
1561     \DisplayProof
1562
1563     \myderivsp
1564
1565     \AxiomC{$\myctx \vdash \mytya \mynf \myfora{\myb{x}}{\mytyb}{\myse{C}}$}
1566     \AxiomC{$\mychkk{\myctx; \myb{x}: \mytyb}{\mytmt}{\myse{C}}$}
1567     \BinaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\mytya}$}
1568     \DisplayProof
1569   }
1570 }
1571
1572 \subsection{Elaboration}
1573
1574 \mydesc{syntax}{ }{
1575   $
1576   \begin{array}{r@{\ }c@{\ }l}
1577       \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
1578                  &  |  & \mypost{\myb{x}}{\mytmsyn} \\
1579                  &  |  & \myadt{\mytyc{D}}{\mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
1580                  &  |  & \myreco{\mytyc{D}}{\mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
1581
1582       \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn)
1583   \end{array}
1584   $
1585 }
1586
1587 \subsubsection{Values and postulated variables}
1588
1589 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
1590   \centering{
1591     \begin{tabular}{cc}
1592       \AxiomC{$\myjud{\mytmt}{\mytya}$}
1593       \AxiomC{$\myfun{f} \not\in \myctx$}
1594       \BinaryInfC{
1595         $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \  \myctx; \myfun{f} \mapsto \mytmt : \mytya$
1596       }
1597       \DisplayProof
1598       &
1599       \AxiomC{$\myjud{\mytya}{\mytyp}$}
1600       \AxiomC{$\myfun{f} \not\in \myctx$}
1601       \BinaryInfC{
1602         $
1603           \myctx \myelabt \mypost{\myfun{f}}{\mytya}
1604           \ \ \myelabf\ \  \myctx; \myfun{f} : \mytya
1605         $
1606       }
1607       \DisplayProof
1608     \end{tabular}
1609 }
1610 }
1611
1612 \subsubsection{User defined types}
1613
1614 \mydesc{syntax}{ }{
1615   $
1616   \begin{array}{l}
1617     \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
1618   \end{array}
1619   $
1620 }
1621
1622 \subsubsection{Data types}
1623
1624 \begin{figure}[t]
1625   \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
1626     \centering{
1627       $
1628       \begin{array}{r@{\ }c@{\ }l}
1629         \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \myvec{(\myb{x} {:} \mytya)} \ |\ \cdots } \\
1630         & \myelabf &
1631         
1632         \begin{array}{r@{\ }c@{\ }l}
1633           \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\myvec{\mytmsyn}} \mysynsep
1634           \mytyc{D}.\mydc{c}_n \myappsp \myvec{\mytmsyn} \mysynsep  \cdots \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
1635         \end{array}
1636       \end{array}
1637       $
1638     }
1639   }
1640
1641   \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
1642     \centering{
1643       \AxiomC{$\myinf{\mytele \myarr \mytyp}{\mytyp}$}
1644       \AxiomC{$\mytyc{D} \not\in \myctx$}
1645       \noLine
1646       \BinaryInfC{$\myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n)$}
1647       \noLine
1648       \UnaryInfC{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
1649       \UnaryInfC{$
1650         \begin{array}{r@{\ }c@{\ }l}
1651           \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \mydc{c} : \mytele_1 \ |\ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
1652           & & \vspace{-0.2cm} \\
1653           & \myelabf & \myctx;\ \mytyc{D} : \mytele \mycc \mytyp;\ \mytyc{D}.\mydc{c}_1 : \mytele \mycc \mytele_1 \myarr \myapp{\mytyc{D}}{\mytelee};\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
1654           &          &
1655           \begin{array}{@{}r@{\ }l l}
1656             \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
1657             & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
1658             & \left.
1659               \begin{array}{@{}l}
1660                 (\mytele_1 \mycc \myhyps(\myb{P}, \mytele_1) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_1}{\mytelee_1})}) \myarr \\
1661                 \myind{3} \vdots \\
1662                 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
1663               \end{array} \right \}
1664             & \textbf{methods}  \\
1665             & \myapp{\myb{P}}{\myb{x}} &
1666           \end{array} \\
1667           \\
1668           \multicolumn{3}{l}{
1669         \begin{array}{@{}l l@{\ } l@{} r c l}
1670           \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
1671           & \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) \\
1672           & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
1673         \end{array}
1674         }
1675         \end{array}
1676         $}
1677       \DisplayProof
1678     }
1679   }
1680
1681   \mydesc{reduction elaboration:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{  
1682     \centering{
1683       \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
1684       \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
1685       \BinaryInfC{$
1686         \begin{array}{c}
1687           \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)} \\ \\
1688         \begin{array}{@{}l l@{\ } l@{} r c l}
1689           \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
1690                          & \myrecs(\myse{P}, \vec{m}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{t}}); \mytele & ) & \mymetagoes &  (\mytyc{D}.\myfun{elim} \myappsp \myb{r} \myappsp \myse{P} \myappsp \vec{m}); \myrecs(\myse{P}, \vec{m}, \mytele) \\
1691                          & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
1692           \end{array}
1693         \end{array}
1694         $}
1695       \DisplayProof
1696     }
1697   }
1698
1699   \caption{Elaborations for data types.}
1700   \label{fig:elab-adt}
1701 \end{figure}
1702
1703
1704 \subsubsection{Records}
1705
1706 \begin{figure}[t]
1707 \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
1708   \centering{
1709     $
1710     \begin{array}{r@{\ }c@{\ }l}
1711       \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \myvec{(\myb{x} {:} \mytya)} \ |\ \cdots } \\
1712              & \myelabf &
1713
1714              \begin{array}{r@{\ }c@{\ }l}
1715                \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\myvec{\mytmsyn}} \mysynsep
1716                                 \mytyc{D}.\mydc{c}_n \myappsp \myvec{\mytmsyn} \mysynsep  \cdots \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
1717              \end{array}
1718     \end{array}
1719     $
1720   }
1721 }
1722
1723
1724 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
1725   \centering{
1726     \AxiomC{$\myinf{\mytele \myarr \mytyp}{\mytyp}$}
1727     \AxiomC{$\mytyc{D} \not\in \myctx$}
1728     \noLine
1729     \BinaryInfC{$\myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)$}
1730     \UnaryInfC{$
1731       \begin{array}{r@{\ }c@{\ }l}
1732         \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \myfun{f}_1 : \myse{F}_1, \cdots, \myfun{f}_n : \myse{F}_n } \\
1733         & & \vspace{-0.2cm} \\
1734         & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\\
1735         & & \mytyc{D}.\myfun{f}_1 : \mytele \myarr \myapp{\mytyc{D}}{\mytelee} \myarr \myse{F}_1;\ \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}; \\
1736         & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
1737       \end{array}
1738       $}
1739     \DisplayProof
1740   }
1741 }
1742
1743   \mydesc{reduction elaboration:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
1744     \centering{
1745       \AxiomC{$\mytyc{D} \in \myctx$}
1746       \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
1747       \DisplayProof
1748     }
1749   }
1750
1751   \caption{Elaborations for records.}
1752   \label{fig:elab-adt}
1753 \end{figure}
1754
1755
1756 \subsection{Type hierarchy}
1757 \label{sec:term-hierarchy}
1758
1759 \subsection{Defined and postulated variables}
1760
1761 As mentioned, in \mykant\ we can defined top level variables, with or without
1762 a body.  We call the variables
1763
1764 \subsection{Observational equality}
1765
1766 \section{\mykant : The practice}
1767 \label{sec:kant-practice}
1768
1769 The codebase consists of around 2500 lines of Haskell, as reported by the
1770 \texttt{cloc} utility.  The high level design is heavily inspired by Conor
1771 McBride's work on various incarnations of Epigram, and specifically by the
1772 first version as described \citep{McBride2004} and the codebase for the new
1773 version \footnote{Available intermittently as a \texttt{darcs} repository at
1774   \url{http://sneezy.cs.nott.ac.uk/darcs/Pig09}.}.  In many ways \mykant\ is
1775 something in between the first and second version of Epigram.
1776
1777 The interaction happens in a read-eval-print loop (REPL).  The repl is a
1778 available both as a commandline application and in a web interface, which is
1779 available at \url{kant.mazzo.li} and presents itself as in figure
1780 \ref{fig:kant-web}.
1781
1782 \begin{figure}
1783   \centering{
1784     \includegraphics[scale=1.0]{kant-web.png}
1785   }
1786   \caption{The \mykant\ web prompt.}
1787   \label{fig:kant-web}
1788 \end{figure}
1789
1790 The interaction with the user takes place in a loop living in and updating a
1791 context \mykant\ declarations.  The user inputs a new declaration that goes
1792 through various stages starts with the user inputing a \mykant\ declaration or
1793 another REPL command, which then goes through various stages that can end up
1794 in a context update, or in failures of various kind.  The process is described
1795 diagrammatically in figure \ref{fig:kant-process}:
1796
1797 \begin{description}
1798 \item[Parse] In this phase the text input gets converted to a sugared
1799   version of the core language.
1800
1801 \item[Desugar] The sugared declaration is converted to a core term.
1802
1803 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
1804   which is necessary to implement the type hierarchy check.
1805
1806 \item[Elaborate] Convert the declaration to some context item, which might be
1807   a value declaration (type and body) or a data type declaration (constructors
1808   and destructors).  This phase works in tandem with \textbf{Typechecking},
1809   which in turns needs to \textbf{Evaluate} terms.
1810
1811 \item[Distill] and report the result.  `Distilling' refers to the process of
1812   converting a core term back to a sugared version that the user can
1813   visualise.  This can be necessary both to display errors including terms or
1814   to display result of evaluations or type checking that the user has
1815   requested.
1816
1817 \item[Pretty print] Format the terms in a nice way, and display the result to
1818   the user.
1819
1820 \end{description}
1821
1822 The details of each phase will be described in section % TODO insert section
1823       
1824 \begin{figure}
1825   \centering{\small
1826     \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
1827     corners, minimum height=2.5em, node distance=0.7cm]
1828       
1829       \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
1830       centered, inner sep=0pt, node distance=0.7cm]
1831       
1832       \tikzstyle{line} = [draw, -latex']
1833       
1834       \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
1835       centered, node distance=1.5cm]
1836       
1837       
1838       \begin{tikzpicture}[auto]
1839         \node [cloud] (user) {User};
1840         \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
1841         \node [block, below=of parse] (desugar) {Desugar};
1842         \node [block, below=of desugar] (reference) {Reference};
1843         \node [block, below=of reference] (elaborate) {Elaborate};
1844         \node [block, left=of elaborate] (tycheck) {Typecheck};
1845         \node [block, left=of tycheck] (evaluate) {Evaluate};
1846         \node [decision, right=of elaborate] (error) {Error?};
1847         \node [block, right=of parse] (distill) {Distill};
1848         \node [block, right=of desugar] (update) {Update context};
1849         
1850         \path [line] (user) -- (parse);
1851         \path [line] (parse) -- (desugar);
1852         \path [line] (desugar) -- (reference);
1853         \path [line] (reference) -- (elaborate);
1854         \path [line] (elaborate) edge[bend right] (tycheck);
1855         \path [line] (tycheck) edge[bend right] (elaborate);
1856         \path [line] (elaborate) -- (error);
1857         \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
1858         \path [line] (error) -- node [near start] {no} (update);
1859         \path [line] (update) -- (distill);
1860         \path [line] (distill) -- (user);
1861         \path [line] (tycheck) edge[bend right] (evaluate);
1862         \path [line] (evaluate) edge[bend right] (tycheck);
1863       \end{tikzpicture}
1864   }
1865   \caption{High level overview of the life of a \mykant\ prompt cycle.}
1866   \label{fig:kant-process}
1867 \end{figure}
1868
1869 \subsection{Term representation}
1870 \label{sec:term-repr}
1871
1872 \subsection{Type hierarchy}
1873
1874 \subsection{Elaboration}
1875
1876 \section{Evaluation}
1877
1878 \section{Future work}
1879
1880 \appendix
1881
1882 \section{Notation and syntax}
1883
1884 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
1885 the type of relation being established and the syntactic elements appearing,
1886 for example
1887
1888 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
1889   \centering{Typing derivations here.}
1890 }
1891
1892 In the languages presented and Agda code samples I also highlight the syntax,
1893 following a uniform color and font convention:
1894
1895 \begin{center}
1896   \begin{tabular}{c | l}
1897     $\mytyc{Sans}$   & Type constructors. \\
1898     $\mydc{sans}$    & Data constructors. \\
1899     % $\myfld{sans}$  & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
1900     $\mysyn{roman}$  & Keywords of the language. \\
1901     $\myfun{roman}$  & Defined values and destructors. \\
1902     $\myb{math}$     & Bound variables.
1903   \end{tabular}
1904 \end{center}
1905
1906 Moreover, I will from time to time give examples in the Haskell programming
1907 language as defined in \citep{Haskell2010}, which I will typeset in
1908 \texttt{teletype} font.  I assume that the reader is already familiar with
1909 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
1910
1911 When presenting grammars, I will use a word in $\mysynel{math}$ font
1912 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate nonterminals. Additionally,
1913 I will use quite flexibly a $\mysynel{math}$ font to indicate a syntactic
1914 element.  More specifically, terms are usually indicated by lowercase letters
1915 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase letter (often
1916 $\mytya$, $\mytyb$, or $\mytycc$).
1917
1918 When presenting type derivations, I will often abbreviate and present multiple
1919 conclusions, each on a separate line:
1920
1921 \begin{prooftree}
1922   \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
1923   \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
1924   \noLine
1925   \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
1926 \end{prooftree}
1927
1928 \section{Agda rendition of core ITT}
1929 \label{app:agda-code}
1930
1931 \begin{code}
1932 module ITT where
1933   open import Level
1934
1935   data ⊥ : Set where
1936
1937   absurd : ∀ {a} {A : Set a} → ⊥ → A
1938   absurd ()
1939
1940   record ⊤ : Set where
1941     constructor tt
1942
1943   record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
1944     constructor _,_
1945     field
1946       fst  : A
1947       snd  : B fst
1948
1949   data Bool : Set where
1950     true false : Bool
1951
1952   if_then_else_ : ∀ {a} {P : Bool → Set a} (x : Bool) → P true → P false → P x
1953   if true then x else _ = x
1954   if false then _ else x = x
1955
1956   data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
1957     _◁_ : (s : S) → (P s → W S P) → W S P
1958
1959   rec : ∀ {a b} {S : Set a} {P : S → Set b}
1960     (C : W S P → Set) →       -- some conclusion we hope holds
1961     ((s : S) →                -- given a shape...
1962      (f : P s → W S P) →      -- ...and a bunch of kids...
1963      ((p : P s) → C (f p)) →  -- ...and C for each kid in the bunch...
1964      C (s ◁ f)) →             -- ...does C hold for the node?
1965     (x : W S P) →             -- If so, ...
1966     C x                       -- ...C always holds.
1967   rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
1968
1969   data _≡_ {a} {A : Set a} : A → A → Set a where
1970     refl : ∀ x → x ≡ x
1971
1972   ≡-elim : ∀ {a b} {A : Set a}
1973     (P : (x y : A) → x ≡ y → Set b) →
1974     ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
1975   ≡-elim P p (refl x) = p
1976
1977   subst : ∀ {a b} {A : Set a}
1978     (P : A → Set b) →
1979     ∀ {x y} → (x≡y : x ≡ y) → P x → P y
1980   subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
1981 \end{code}
1982
1983 \nocite{*}
1984 \bibliographystyle{authordate1}
1985 \bibliography{thesis}
1986
1987 \end{document}