1 \documentclass[report]{article}
5 % \usepackage{fullpage}
17 \usepackage[fleqn]{amsmath}
18 \usepackage{stmaryrd} %llbracket
21 \usepackage{bussproofs}
33 \usepackage{subcaption}
39 \usetikzlibrary{shapes,arrows,positioning}
40 % \usepackage{tikz-cd}
41 % \usepackage{pgfplots}
44 %% -----------------------------------------------------------------------------
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}
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}}
61 \renewenvironment{code}%
62 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed\small}%
63 {\endpboxed\par\noindent%
64 \ignorespacesafterend\small}
67 %% -----------------------------------------------------------------------------
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}
90 \newcommand{\mydesc}[3]{
96 \hfill \textbf{#1} $#2$
97 \framebox[\textwidth]{
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{\mybigand}{\mathrel{\mytyc{$\bigwedge$}}}
178 \newcommand{\myprfora}[3]{\forall #1 {:} #2. #3}
179 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
180 \newcommand{\mybot}{\mytyc{$\bot$}}
181 \newcommand{\mytop}{\mytyc{$\top$}}
182 \newcommand{\mycoe}{\myfun{coe}}
183 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
184 \newcommand{\mycoh}{\myfun{coh}}
185 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
186 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
187 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
188 \newcommand{\myprop}{\mytyc{Prop}}
189 \newcommand{\mytmup}{\mytmsyn\uparrow}
190 \newcommand{\mydefs}{\Delta}
191 \newcommand{\mynf}{\Downarrow}
192 \newcommand{\myinff}[3]{#1 \vdash #2 \Rightarrow #3}
193 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
194 \newcommand{\mychkk}[3]{#1 \vdash #2 \Leftarrow #3}
195 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
196 \newcommand{\myann}[2]{#1 : #2}
197 \newcommand{\mydeclsyn}{\myse{decl}}
198 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
199 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
200 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
201 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ \{ #4 \}}
202 \newcommand{\myelabt}{\vdash}
203 \newcommand{\myelabf}{\rhd}
204 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
205 \newcommand{\mytele}{\Delta}
206 \newcommand{\mytelee}{\delta}
207 \newcommand{\mydcctx}{\Gamma}
208 \newcommand{\mynamesyn}{\myse{name}}
209 \newcommand{\myvec}{\overrightarrow}
210 \newcommand{\mymeta}{\textsc}
211 \newcommand{\myhyps}{\mymeta{hyps}}
212 \newcommand{\mycc}{;}
213 \newcommand{\myemptytele}{\cdot}
214 \newcommand{\mymetagoes}{\Longrightarrow}
215 % \newcommand{\mytesctx}{\
216 \newcommand{\mytelesyn}{\myse{telescope}}
217 \newcommand{\myrecs}{\mymeta{recs}}
218 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
219 \newcommand{\mylet}{\mysyn{let}}
220 \newcommand{\myhead}{\mymeta{head}}
221 \newcommand{\mytake}{\mymeta{take}}
222 \newcommand{\myix}{\mymeta{ix}}
223 \newcommand{\myapply}{\mymeta{apply}}
224 \newcommand{\mydataty}{\mymeta{datatype}}
225 \newcommand{\myisreco}{\mymeta{record}}
226 \newcommand{\mydcsep}{\ |\ }
227 \newcommand{\mytree}{\mytyc{Tree}}
228 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
229 \newcommand{\mysigma}{\mytyc{$\Sigma$}}
232 %% -----------------------------------------------------------------------------
234 \title{\mykant: Implementing Observational Equality}
235 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
250 \thispagestyle{empty}
252 \begin{minipage}{0.4\textwidth}
253 \begin{flushleft} \large
255 Dr. Steffen \textsc{van Backel}
258 \begin{minipage}{0.4\textwidth}
259 \begin{flushright} \large
261 Dr. Philippa \textsc{Gardner}
268 The marriage between programming and logic has been a very fertile one. In
269 particular, since the simply typed lambda calculus (STLC), a number of type
270 systems have been devised with increasing expressive power.
272 Among this systems, Inutitionistic Type Theory (ITT) has been a very
273 popular framework for theorem provers and programming languages.
274 However, equality has always been a tricky business in ITT and related
277 In these thesis we will explain why this is the case, and present
278 Observational Type Theory (OTT), a solution to some of the problems
279 with equality. We then describe $\mykant$, a theorem prover featuring
280 OTT in a setting more close to the one found in current systems.
281 Having implemented part of $\mykant$ as a Haskell program, we describe
282 some of the implementation issues faced.
287 \renewcommand{\abstractname}{Acknowledgements}
289 I would like to thank Steffen van Backel, my supervisor, who was brave
290 enough to believe in my project and who provided much advice and
293 I would also like to thank the Haskell and Agda community on
294 \texttt{IRC}, which guided me through the strange world of types; and
295 in particular Andrea Vezzosi and James Deikun, with whom I entertained
296 countless insightful discussions in the past year. Andrea suggested
297 Observational Type Theory as a topic of study: this thesis would not
298 exist without him. Before them, Tony Fields introduced me to Haskell,
299 unknowingly filling most of my free time from that time on.
301 Finally, much of the work stems from the research of Conor McBride,
302 who answered many of my doubts through these months. I also owe him
312 \section{Simple and not-so-simple types}
315 \subsection{The untyped $\lambda$-calculus}
317 Along with Turing's machines, the earliest attempts to formalise computation
318 lead to the $\lambda$-calculus \citep{Church1936}. This early programming
319 language encodes computation with a minimal syntax and no `data' in the
320 traditional sense, but just functions. Here we give a brief overview of the
321 language, which will give the chance to introduce concepts central to the
322 analysis of all the following calculi. The exposition follows the one found in
323 chapter 5 of \cite{Queinnec2003}.
325 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
330 \begin{array}{r@{\ }c@{\ }l}
331 \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
332 x & \in & \text{Some enumerable set of symbols}
337 Parenthesis will be omitted in the usual way:
338 $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
339 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
341 Abstractions roughly corresponds to functions, and their semantics is more
342 formally explained by the $\beta$-reduction rule:
344 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
347 \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{, where} \\
349 \begin{array}{l@{\ }c@{\ }l}
350 \mysub{\myb{x}}{\myb{x}}{\mytmn} & = & \mytmn \\
351 \mysub{\myb{y}}{\myb{x}}{\mytmn} & = & y\text{, with } \myb{x} \neq y \\
352 \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & = & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
353 \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{x}}{\mytmm} \\
354 \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}}, \\
355 \multicolumn{3}{l}{\myind{2} \text{with $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
361 The care required during substituting variables for terms is required to avoid
362 name capturing. We will use substitution in the future for other name-binding
363 constructs assuming similar precautions.
365 These few elements are of remarkable expressiveness, and in fact Turing
366 complete. As a corollary, we must be able to devise a term that reduces forever
367 (`loops' in imperative terms):
370 (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
374 A \emph{redex} is a term that can be reduced. In the untyped $\lambda$-calculus
375 this will be the case for an application in which the first term is an
376 abstraction, but in general we call aterm reducible if it appears to the left of
377 a reduction rule. When a term contains no redexes it's said to be in
378 \emph{normal form}. Given the observation above, not all terms reduce to a
379 normal forms: we call the ones that do \emph{normalising}, and the ones that
380 don't \emph{non-normalising}.
382 The reduction rule presented is not syntax directed, but \emph{evaluation
383 strategies} can be employed to reduce term systematically. Common evaluation
384 strategies include \emph{call by value} (or \emph{strict}), where arguments of
385 abstractions are reduced before being applied to the abstraction; and conversely
386 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
387 to proceed---in other words when we have an application where the function is
388 still not a $\lambda$. In both these reduction strategies we never reduce under
389 an abstraction: for this reason a weaker form of normalisation is used, where
390 both abstractions and normal forms are said to be in \emph{weak head normal
393 \subsection{The simply typed $\lambda$-calculus}
395 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
396 \emph{types} to them, and then check that the terms that we are forming make
397 sense given our typing rules \citep{Curry1934}. The first most basic instance
398 of this idea takes the name of \emph{simply typed $\lambda$ calculus}, whose
399 rules are shown in figure \ref{fig:stlc}.
401 Our types contain a set of \emph{type variables} $\Phi$, which might
402 correspond to some `primitive' types; and $\myarr$, the type former for
403 `arrow' types, the types of functions. The language is explicitly
404 typed: when we bring a variable into scope with an abstraction, we
405 declare its type. Reduction is unchanged from the untyped
411 \begin{array}{r@{\ }c@{\ }l}
412 \mytmsyn & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
413 (\myapp{\mytmsyn}{\mytmsyn}) \\
414 \mytysyn & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn \mysynsep \\
415 \myb{x} & \in & \text{Some enumerable set of symbols} \\
416 \myse{\phi} & \in & \Phi
421 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
423 \AxiomC{$\myctx(x) = A$}
424 \UnaryInfC{$\myjud{\myb{x}}{A}$}
427 \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
428 \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
431 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
432 \AxiomC{$\myjud{\mytmn}{\mytya}$}
433 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
437 \caption{Syntax and typing rules for the STLC. Reduction is unchanged from
438 the untyped $\lambda$-calculus.}
442 In the typing rules, a context $\myctx$ is used to store the types of bound
443 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
444 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
446 This typing system takes the name of `simply typed lambda calculus' (STLC), and
447 enjoys a number of properties. Two of them are expected in most type systems
450 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
451 constructor does not appear on the left of the $\myred$ relation (currently
452 only $\lambda$), or it can take a step according to the evaluation rules.
453 \item[Preservation] If a well-typed term takes a step of evaluation, then the
454 resulting term is also well-typed, and preserves the previous type. Also
455 known as \emph{subject reduction}.
458 However, STLC buys us much more: every well-typed term is normalising
459 \citep{Tait1967}. It is easy to see that we can't fill the blanks if we want to
460 give types to the non-normalising term shown before:
462 \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
465 This makes the STLC Turing incomplete. We can recover the ability to loop by
466 adding a combinator that recurses:
469 \begin{minipage}{0.5\textwidth}
471 $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
475 \begin{minipage}{0.5\textwidth}
476 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
477 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
478 \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
483 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
484 $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
487 This will deprive us of normalisation, which is a particularly bad thing if we
488 want to use the STLC as described in the next section.
490 \subsection{The Curry-Howard correspondence}
492 It turns out that the STLC can be seen a natural deduction system for
493 intuitionistic propositional logic. Terms are proofs, and their types are the
494 propositions they prove. This remarkable fact is known as the Curry-Howard
495 correspondence, or isomorphism.
497 The arrow ($\myarr$) type corresponds to implication. If we wish to prove that
498 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
499 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
502 \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
504 That is, function composition. Going beyond arrow types, we can extend our bare
505 lambda calculus with useful types to represent other logical constructs, as
506 shown in figure \ref{fig:natded}.
511 \begin{array}{r@{\ }c@{\ }l}
512 \mytmsyn & ::= & \cdots \\
513 & | & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
514 & | & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
515 \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
516 \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
517 & | & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
518 \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
519 \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
524 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
527 \begin{array}{l@{ }l@{\ }c@{\ }l}
528 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
529 \myapp{\mytmm}{\mytmt} \\
530 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
531 \myapp{\mytmn}{\mytmt}
536 \begin{array}{l@{ }l@{\ }c@{\ }l}
537 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
538 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
544 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
546 \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
547 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
550 \AxiomC{$\myjud{\mytmt}{\myempty}$}
551 \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
558 \AxiomC{$\myjud{\mytmt}{\mytya}$}
559 \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
562 \AxiomC{$\myjud{\mytmt}{\mytyb}$}
563 \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
571 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
572 \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
573 \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
574 \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
581 \AxiomC{$\myjud{\mytmm}{\mytya}$}
582 \AxiomC{$\myjud{\mytmn}{\mytyb}$}
583 \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
586 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
587 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
590 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
591 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
595 \caption{Rules for the extendend STLC. Only the new features are shown, all the
596 rules and syntax for the STLC apply here too.}
600 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
601 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
602 products---$\myprod$ here, tuples in Haskell) correspond to
603 conjunctions. This is apparent looking at the ways to construct and
604 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
605 and $\myright{ }$ correspond to $\vee$ introduction, and
606 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
607 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
608 and $\mysnd$ to $\wedge$ elimination.
610 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
611 $\myempty$ corresponds to the logical $\bot$. $\myunit$ has one introduction
612 rule ($\mytt$), and thus one inhabitant; and no eliminators. $\myempty$ has no
613 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
614 }$), corresponding to the logical \emph{ex falso quodlibet}.
616 With these rules, our STLC now looks remarkably similar in power and use to the
617 natural deduction we already know. $\myneg \mytya$ can be expressed as $\mytya
618 \myarr \myempty$. However, there is an important omission: there is no term of
619 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
620 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
621 a type equivalent to those.
623 This has a considerable effect on our logic and it's no coincidence, since there
624 is no obvious computational behaviour for laws like the excluded middle.
625 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
626 and all the systems analysed will have this characteristic since they build on
627 the foundation of the STLC\footnote{There is research to give computational
628 behaviour to classical logic, but I will not touch those subjects.}.
630 As in logic, if we want to keep our system consistent, we must make sure that no
631 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
632 The variant of STLC presented here is indeed
633 consistent, a result that follows from the fact that it is
634 normalising. % TODO explain
635 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
636 desire for consistency. The following term works for every type $\mytya$,
639 (\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya
642 \subsection{Inductive data}
645 To make the STLC more useful as a programming language or reasoning tool it is
646 common to include (or let the user define) inductive data types. These comprise
647 of a type former, various constructors, and an eliminator (or destructor) that
648 serves as primitive recursor.
650 For example, we might add a $\mylist$ type constructor, along with an `empty
651 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor. The eliminator for
652 lists will be the usual folding operation ($\myfoldr$). See figure
658 \begin{array}{r@{\ }c@{\ }l}
659 \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
661 \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
662 \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
666 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
668 \begin{array}{l@{\ }c@{\ }l}
669 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
671 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
672 \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
676 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
678 \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
679 \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
682 \AxiomC{$\myjud{\mytmm}{\mytya}$}
683 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
684 \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
689 \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
690 \AxiomC{$\myjud{\mytmm}{\mytyb}$}
691 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
692 \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
695 \caption{Rules for lists in the STLC.}
699 In section \ref{sec:well-order} we will see how to give a general account of
700 inductive data. %TODO does this make sense to have here?
702 \section{Intuitionistic Type Theory}
705 \subsection{Extending the STLC}
707 The STLC can be made more expressive in various ways. \cite{Barendregt1991}
708 succinctly expressed geometrically how we can add expressivity:
712 & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
713 & & \lambda C \ar@{-}[dd]
715 \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
716 & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
718 & \lambda\underline\omega \ar@{-}'[r][rr]
719 & & \lambda P\underline\omega
721 \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
722 & & \lambda P \ar@{-}[ur]
725 Here $\lambda{\to}$, in the bottom left, is the STLC. From there can move along
728 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
729 types in our type signatures. For example, we can define a polymorphic
731 {\small\[\displaystyle
732 (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} : \mytyp) \myarr \myb{A} \myarr \myb{A}
734 The first and most famous instance of this idea has been System F. This form
735 of polymorphism and has been wildly successful, also thanks to a well known
736 inference algorithm for a restricted version of System F known as
737 Hindley-Milner. Languages like Haskell and SML are based on this discipline.
738 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
739 type operators. For example we could define a function that given types $R$
740 and $\mytya$ forms the type that represents a value of type $\mytya$ in
741 continuation passing style: {\small\[\displaystyle(\myabss{\myb{A} \myar \myb{R}}{\mytyp}{(\myb{A}
742 \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp\]}
743 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
744 types', give great expressive power. For example, we can have values of whose
745 type depend on a boolean:
746 {\small\[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
750 All the systems preserve the properties that make the STLC well behaved. The
751 system we are going to focus on, Intuitionistic Type Theory, has all of the
752 above additions, and thus would sit where $\lambda{C}$ sits in the
753 `$\lambda$-cube'. It will serve as the logical `core' of all the other
754 extensions that we will present and ultimately our implementation of a similar
757 \subsection{A Bit of History}
759 Logic frameworks and programming languages based on type theory have a long
760 history. Per Martin-L\"{o}f described the first version of his theory in 1971,
761 but then revised it since the original version was inconsistent due to its
762 impredicativity\footnote{In the early version there was only one universe
763 $\mytyp$ and $\mytyp : \mytyp$, see section \ref{sec:term-types} for an
764 explanation on why this causes problems.}. For this reason he gave a revised
765 and consistent definition later \citep{Martin-Lof1984}.
767 A related development is the polymorphic $\lambda$-calculus, and specifically
768 the previously mentioned System F, which was developed independently by Girard
769 and Reynolds. An overview can be found in \citep{Reynolds1994}. The surprising
770 fact is that while System F is impredicative it is still consistent and strongly
771 normalising. \cite{Coquand1986} further extended this line of work with the
772 Calculus of Constructions (CoC).
774 Most widely used interactive theorem provers are based on ITT. Popular ones
775 include Agda \citep{Norell2007, Bove2009}, Coq \citep{Coq}, and Epigram
776 \citep{McBride2004, EpigramTut}.
778 \subsection{A note on inference}
780 % TODO do this, adding links to the sections about bidi type checking and
781 % implicit universes.
782 In the following text I will often omit explicit typing for abstractions or
784 Moreover, I will use $\mytyp$ without bothering to specify a
785 universe, with the silent assumption that the definition is consistent
786 regarding to the hierarchy.
788 \subsection{A simple type theory}
791 The calculus I present follows the exposition in \citep{Thompson1991},
792 and is quite close to the original formulation of predicative ITT as
793 found in \citep{Martin-Lof1984}. The system's syntax and reduction
794 rules are presented in their entirety in figure \ref{fig:core-tt-syn}.
795 The typing rules are presented piece by piece. Agda and \mykant\
796 renditions of the presented theory and all the examples is reproduced in
797 appendix \ref{app:itt-code}.
802 \begin{array}{r@{\ }c@{\ }l}
803 \mytmsyn & ::= & \myb{x} \mysynsep
805 \myunit \mysynsep \mytt \mysynsep
806 \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
807 & | & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
808 \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
809 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
810 \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
811 (\myapp{\mytmsyn}{\mytmsyn}) \\
812 & | & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
813 \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
814 & | & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
815 & | & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
816 \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
817 & | & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
823 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
826 \begin{array}{l@{ }l@{\ }c@{\ }l}
827 \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
828 \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
833 \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
837 \begin{array}{l@{ }l@{\ }c@{\ }l}
838 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
839 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
847 \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
848 \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
849 \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
853 \caption{Syntax and reduction rules for our type theory.}
854 \label{fig:core-tt-syn}
857 \subsubsection{Types are terms, some terms are types}
858 \label{sec:term-types}
860 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
862 \AxiomC{$\myjud{\mytmt}{\mytya}$}
863 \AxiomC{$\mytya \mydefeq \mytyb$}
864 \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
867 \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
868 \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
873 The first thing to notice is that a barrier between values and types that we had
874 in the STLC is gone: values can appear in types, and the two are treated
875 uniformly in the syntax.
877 While the usefulness of doing this will become clear soon, a consequence is
878 that since types can be the result of computation, deciding type equality is
879 not immediate as in the STLC. For this reason we define \emph{definitional
880 equality}, $\mydefeq$, as the congruence relation extending
881 $\myred$---moreover, when comparing types syntactically we do it up to
882 renaming of bound names ($\alpha$-renaming). For example under this
883 discipline we will find that
885 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}}
887 Types that are definitionally equal can be used interchangeably. Here
888 the `conversion' rule is not syntax directed, but it is possible to
889 employ $\myred$ to decide term equality in a systematic way, by always
890 reducing terms to their normal forms before comparing them, so that a
891 separate conversion rule is not needed. % TODO add section
892 Another thing to notice is that considering the need to reduce terms to
893 decide equality, it is essential for a dependently type system to be
894 terminating and confluent for type checking to be decidable.
896 Moreover, we specify a \emph{type hierarchy} to talk about `large'
897 types: $\mytyp_0$ will be the type of types inhabited by data:
898 $\mybool$, $\mynat$, $\mylist$, etc. $\mytyp_1$ will be the type of
899 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
900 \mytyp_0 : \mytyp_1 : \cdots$. Each type `level' is often called a
901 universe in the literature. While it is possible to simplify things by
902 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
903 inconsistent for much the same reason that impredicative na\"{\i}ve set
904 theory is \citep{Hurkens1995}. However various techniques can be
905 employed to lift the burden of explicitly handling universes, as we will
906 see in section \ref{sec:term-hierarchy}.
908 \subsubsection{Contexts}
910 \begin{minipage}{0.5\textwidth}
911 \mydesc{context validity:}{\myvalid{\myctx}}{
913 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
914 \UnaryInfC{$\myvalid{\myemptyctx}$}
917 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
918 \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
923 \begin{minipage}{0.5\textwidth}
924 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
925 \AxiomC{$\myctx(x) = \mytya$}
926 \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
932 We need to refine the notion context to make sure that every variable appearing
933 is typed correctly, or that in other words each type appearing in the context is
934 indeed a type and not a value. In every other rule, if no premises are present,
935 we assume the context in the conclusion to be valid.
937 Then we can re-introduce the old rule to get the type of a variable for a
940 \subsubsection{$\myunit$, $\myempty$}
942 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
944 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
945 \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
947 \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
950 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
951 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
953 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
956 \AxiomC{$\myjud{\mytmt}{\myempty}$}
957 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
958 \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
960 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
965 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
966 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
967 sure that we are invoking $\myabsurd{}$ over a type.
969 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
971 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
974 \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
978 \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
982 \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
987 \AxiomC{$\myjud{\mytmt}{\mybool}$}
988 \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
990 \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
991 \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
995 With booleans we get the first taste of the `dependent' in `dependent
996 types'. While the two introduction rules ($\mytrue$ and $\myfalse$) are
997 not surprising, the typing rules for $\myfun{if}$ are. In most strongly
998 typed languages we expect the branches of an $\myfun{if}$ statements to
999 be of the same type, to preserve subject reduction, since execution
1000 could take both paths. This is a pity, since the type system does not
1001 reflect the fact that in each branch we gain knowledge on the term we
1002 are branching on. Which means that programs along the lines of
1003 {\small\[\text{\texttt{if null xs then head xs else 0}}\]}
1004 are a necessary, well typed, danger.
1006 However, in a more expressive system, we can do better: the branches' type can
1007 depend on the value of the scrutinised boolean. This is what the typing rule
1008 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1009 representing the scrutinised boolean type, and the branches are typechecked with
1010 the updated knowledge on the value of $\myb{x}$.
1012 \subsubsection{$\myarr$, or dependent function}
1014 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1015 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1016 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1017 \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1023 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1024 \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1027 \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1028 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1029 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1034 Dependent functions are one of the two key features that perhaps most
1035 characterise dependent types---the other being dependent products. With
1036 dependent functions, the result type can depend on the value of the
1037 argument. This feature, together with the fact that the result type
1038 might be a type itself, brings a lot of interesting possibilities.
1039 Following this intuition, in the introduction rule, the return type is
1040 typechecked in a context with an abstracted variable of lhs' type, and
1041 in the elimination rule the actual argument is substituted in the return
1042 type. Keeping the correspondence with logic alive, dependent functions
1043 are much like universal quantifiers ($\forall$) in logic.
1045 For example, assuming that we have lists and natural numbers in our
1046 language, using dependent functions we would be able to
1050 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1051 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1052 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1053 \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{>}} 0 \myarr
1058 \myfun{length} is the usual polymorphic length function. $\myfun{>}$ is
1059 a function that takes two naturals and returns a type: if the lhs is
1060 greater then the rhs, $\myunit$ is returned, $\myempty$ otherwise. This
1061 way, we can express a `non-emptyness' condition in $\myfun{head}$, by
1062 including a proof that the length of the list argument is non-zero.
1063 This allows us to rule out the `empty list' case, so that we can safely
1064 return the first element.
1066 Again, we need to make sure that the type hierarchy is respected, which is the
1067 reason why a type formed by $\myarr$ will live in the least upper bound of the
1068 levels of argument and return type. This trend will continue with the other
1069 type-level binders, $\myprod$ and $\mytyc{W}$.
1071 \subsubsection{$\myprod$, or dependent product}
1074 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1075 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1076 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1077 \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1083 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1084 \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1085 \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1087 \UnaryInfC{\phantom{$--$}}
1090 \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1091 \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1093 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1098 If dependent functions are a generalisation of $\myarr$ in the STLC,
1099 dependent products are a generalisation of $\myprod$ in the STLC. The
1100 improvement is that the second element's type can depend on the value of
1101 the first element. The corrispondence with logic is through the
1102 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1103 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1104 The first element will be a number, and the second evidence that the
1105 number is even. This highlights the fact that we are working in a
1106 constructive logic: if we have an existence proof, we can always ask for
1107 a witness. This means, for instance, that $\neg \forall \neg$ is not
1108 equivalent to $\exists$.
1110 Another perhaps more `dependent' application of products, paired with
1111 $\mybool$, is to offer choice between different types. For example we
1112 can easily recover disjunctions:
1115 \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1116 \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1117 \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} \\
1118 \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1119 \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}})}
1123 \subsubsection{$\mytyc{W}$, or well-order}
1124 \label{sec:well-order}
1126 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1127 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1128 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1129 \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1134 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1135 \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1136 \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1141 \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1142 \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1144 \BinaryInfC{$\myjud{\myse{p}}{
1145 \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}}}}
1147 \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1151 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1152 let us represent inductive data in a general (but clumsy) way. The core
1156 \section{The struggle for equality}
1157 \label{sec:equality}
1159 In the previous section we saw how a type checker (or a human) needs a
1160 notion of \emph{definitional equality}. Beyond this meta-theoretic
1161 notion, in this section we will explore the ways of expressing equality
1162 \emph{inside} the theory, as a reasoning tool available to the user.
1163 This area is the main concern of this thesis, and in general a very
1164 active research topic, since we do not have a fully satisfactory
1165 solution, yet. As in the previous section, everything presented is
1166 formalised in Agda in appendix \ref{app:agda-itt}.
1168 \subsection{Propositional equality}
1171 \begin{minipage}{0.5\textwidth}
1174 \begin{array}{r@{\ }c@{\ }l}
1175 \mytmsyn & ::= & \cdots \\
1176 & | & \mytmsyn \mypeq{\mytmsyn} \mytmsyn \mysynsep
1177 \myapp{\myrefl}{\mytmsyn} \\
1178 & | & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1183 \begin{minipage}{0.5\textwidth}
1184 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1186 \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1192 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1193 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1194 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1195 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1196 \TrinaryInfC{$\myjud{\mytmm \mypeq{\mytya} \mytmn}{\mytyp_l}$}
1202 \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1203 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mytmm \mypeq{\mytya} \mytmn}$}
1208 \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\myb{x} \mypeq{\mytya} \myb{y}}{\mytyp_l}}} \\
1209 \myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1212 \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1217 To express equality between two terms inside ITT, the obvious way to do so is
1218 to have the equality construction to be a type-former. Here we present what
1219 has survived as the dominating form of equality in systems based on ITT up to
1222 Our type former is $\mypeq{\mytya}$, which given a type (in this case
1223 $\mytya$) relates equal terms of that type. $\mypeq{}$ has one introduction
1224 rule, $\myrefl$, which introduces an equality relation between definitionally
1227 Finally, we have one eliminator for $\mypeq{}$, $\myjeqq$. $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1229 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1230 $\mytya$) and a proof of their equality
1231 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1232 $\myse{n}$) are equal
1233 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$, plus
1234 the trivial proof by reflexivity showing that $\myse{m}$ is equal to itself
1236 Given these ingredients, $\myjeqq$ retuns a member of $\myse{P}$ applied to
1237 $\mytmm$, $\mytmn$, and $\myse{q}$. In other words $\myjeqq$ takes a
1238 witness that $\myse{P}$ works with \emph{definitionally equal} terms, and
1239 returns a witness of $\myse{P}$ working with \emph{propositionally equal}
1240 terms. Invokations of $\myjeqq$ will vanish when the equality proofs will
1241 reduce to invocations to reflexivity, at which point the arguments must be
1242 definitionally equal, and thus the provided
1243 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1246 While the $\myjeqq$ rule is slightly convoluted, ve can derive many more
1247 `friendly' rules from it, for example a more obvious `substitution' rule, that
1248 replaces equal for equal in predicates:
1251 \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}}}}} \\
1252 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1253 \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1256 Once we have $\myfun{subst}$, we can easily prove more familiar laws regarding
1257 equality, such as symmetry, transitivity, and a congruence law.
1261 \subsection{Common extensions}
1263 Our definitional equality can be made larger in various ways, here we
1264 review some common extensions.
1266 \subsubsection{Congruence laws and $\eta$-expansion}
1268 A simple type-directed check that we can do on functions and records is
1269 $\eta$-expansion. We can then have
1271 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1273 \AxiomC{$\myjud{f \mydefeq (\myabss{\myb{x}}{\mytya}{\myapp{\myse{g}}{\myb{x}}})}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1274 \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1277 \AxiomC{$\myjud{\mytmm \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1278 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1284 \AxiomC{$\myjud{\mytmm}{\myunit}$}
1285 \AxiomC{$\myjud{\mytmn}{\myunit}$}
1286 \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1290 % \mydesc{definitional equality:}{\mytmsyn \mydefeq \mytmsyn}{
1291 % \begin{tabular}{cc}
1299 \subsubsection{Uniqueness of identity proofs}
1301 % TODO reference the fact that J does not imply J
1302 % TODO mention univalence
1305 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1308 \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\myb{x} \mypeq{\mytya} \myb{x} \myarr \mytyp}} \\\
1309 \myjud{\myse{p}}{\myfora{\myb{x}}{\mytya}{\myse{P} \myappsp \myb{x} \myappsp \myb{x} \myappsp (\myrefl \myapp \myb{x})}} \hspace{1cm}
1310 \myjud{\mytmt}{\mytya} \hspace{1cm}
1311 \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1314 \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{p} \myappsp \myse{t} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1318 \subsection{Limitations}
1320 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1321 schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1323 However, propositional equality as described is quite restricted when
1324 reasoning about equality beyond the term structure, which is what definitional
1325 equality gives us (extension notwithstanding).
1327 The problem is best exemplified by \emph{function extensionality}. In
1328 mathematics, we would expect to be able to treat functions that give equal
1329 output for equal input as the same. When reasoning in a mechanised framework
1330 we ought to be able to do the same: in the end, without considering the
1331 operational behaviour, all functions equal extensionally are going to be
1332 replaceable with one another.
1334 However this is not the case, or in other words with the tools we have we have
1337 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1338 \myb{A} \myarr \myb{B}}{
1339 (\myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{\myb{B}} \myapp{\myb{g}}{\myb{x}}}) \myarr
1340 \myb{f} \mypeq{\myb{A} \myarr \myb{B}} \myb{g}
1344 To see why this is the case, consider the functions
1345 {\small\[\myabs{\myb{x}}{0 \mathrel{\myfun{+}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{+}} 0}\]}
1346 where $\myfun{+}$ is defined by recursion on the first argument,
1347 gradually destructing it to build up successors of the second argument.
1348 The two functions are clearly extensionally equal, and we can in fact
1351 \myfora{\myb{x}}{\mynat}{(0 \mathrel{\myfun{+}} \myb{x}) \mypeq{\mynat} (\myb{x} \mathrel{\myfun{+}} 0)}
1353 By analysis on the $\myb{x}$. However, the two functions are not
1354 definitionally equal, and thus we won't be able to get rid of the
1357 For the reasons above, theories that offer a propositional equality
1358 similar to what we presented are called \emph{intensional}, as opposed
1359 to \emph{extensional}. Most systems in wide use today (such as Agda,
1360 Coq, and Epigram) are of this kind.
1362 This is quite an annoyance that often makes reasoning awkward to execute. It
1363 also extends to other fields, for example proving bisimulation between
1364 processes specified by coinduction, or in general proving equivalences based
1365 on the behaviour on a term.
1367 \subsection{Equality reflection}
1369 One way to `solve' this problem is by identifying propositional equality with
1370 definitional equality:
1372 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1373 \AxiomC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}$}
1374 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1378 This rule takes the name of \emph{equality reflection}, and is a very
1379 different rule from the ones we saw up to now: it links a typing judgement
1380 internal to the type theory to a meta-theoretic judgement that the type
1381 checker uses to work with terms. It is easy to see the dangerous consequences
1384 \item The rule is syntax directed, and the type checker is presumably expected
1385 to come up with equality proofs when needed.
1386 \item More worryingly, type checking becomes undecidable also because
1387 computing under false assumptions becomes unsafe.
1388 Consider for example
1390 \myabss{\myb{q}}{\mytya \mypeq{\mytyp} (\mytya \myarr \mytya)}{\myhole{?}}
1392 Using the assumed proof in tandem with equality reflection we could easily
1393 write a classic Y combinator, sending the compiler into a loop.
1396 Given these facts theories employing equality reflection, like NuPRL
1397 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1398 to keep the systems manageable. % TODO more info, problems with that.
1400 For all its faults, equality reflection does allow us to prove extensionality,
1401 using the extensions we gave above. Assuming that $\myctx$ contains
1402 {\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}}}\]}
1406 \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}$}
1407 \RightLabel{equality reflection}
1408 \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1409 \RightLabel{congruence for $\lambda$s}
1410 \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1411 \RightLabel{$\eta$-law for $\lambda$}
1412 \UnaryInfC{$\hspace{1.45cm}\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}\hspace{1.45cm}$}
1413 \RightLabel{$\myrefl$}
1414 \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\myb{f} \mypeq{} \myb{g}}$}
1417 Now, the question is: do we need to give up well-behavedness of our theory to
1418 gain extensionality?
1420 \subsection{Some alternatives}
1422 % TODO add `extentional axioms' (Hoffman), setoid models (Thorsten)
1424 \section{Observational equality}
1427 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1428 Theory} (OTT), promises to keep the well behavedness of ITT while
1429 being able to gain many useful equality proofs\footnote{It is suspected
1430 that OTT gains \emph{all} the equality proofs of ETT, but no proof
1431 exists yet.}, including function extensionality. The main idea is to
1432 give the user the possibility to \emph{coerce} (or transport) values
1433 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1434 structurally that $\mytya$ and $\mytya$ are equal; and providing a
1435 value-level equality based on similar principles. Here we give an
1436 exposition which follows closely the original paper.
1438 \subsection{A simpler theory, a propositional fragment}
1441 $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1443 \begin{array}{r@{\ }c@{\ }l}
1444 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1445 \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1446 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1447 \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1452 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1454 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1455 \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1458 \AxiomC{$\myjud{\mytmt}{\mybool}$}
1459 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1460 \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1461 \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1466 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1468 \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1469 \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1471 \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1474 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1475 \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1476 \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1478 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1484 \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1485 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1486 \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1490 Our foundation will be a type theory like the one of section
1491 \ref{sec:itt}, with only one level: $\mytyp_0$. In this context we will
1492 drop the $0$ and call $\mytyp_0$ $\mytyp$. Moreover, since the old
1493 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1494 types thanks to the hierarchy (which is gone), we need to reintroduce an
1495 ad-hoc conditional for types, where the reduction rule is the obvious
1498 However, we have an addition: a universe of \emph{propositions},
1499 $\myprop$. $\myprop$ isolates a fragment of types at large, and
1500 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$: \\
1501 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
1504 \begin{array}{l@{\ }c@{\ }l}
1505 \myprdec{\mybot} & \myred & \myempty \\
1506 \myprdec{\mytop} & \myred & \myunit
1511 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
1512 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
1513 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
1514 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
1519 Propositions are what we call the types of \emph{proofs}, or types
1520 whose inhabitants contain no `data', much like $\myunit$. The goal of
1521 doing this is twofold: erasing all top-level propositions when
1522 compiling; and to identify all equivalent propositions as the same, as
1525 Why did we choose what we have in $\myprop$? Given the above
1526 criteria, $\mytop$ obviously fits the bill. A pair of propositions
1527 $\myse{P} \myand \myse{Q}$ still won't get us data. Finally, if
1528 $\myse{P}$ is a proposition and we have
1529 $\myprfora{\myb{x}}{\mytya}{\myse{P}}$ , the decoding will be a
1530 function which returns propositional content. The only threat is
1531 $\mybot$, by which we can fabricate anything we want: however if we
1532 are consistent there will be nothing of type $\mybot$ at the top
1533 level, which is what we care about regarding proof erasure.
1535 \subsection{Equality proofs}
1539 \begin{array}{r@{\ }c@{\ }l}
1540 \mytmsyn & ::= & \cdots \mysynsep
1541 \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
1542 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1543 \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
1544 \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
1549 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1551 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1552 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1553 \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
1556 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1557 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1558 \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
1564 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1569 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
1572 \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
1577 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
1578 \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
1581 \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
1588 While isolating a propositional universe as presented can be a useful
1589 exercises on its own, what we are really after is a useful notion of
1590 equality. In OTT we want to maintain the notion that things judged to
1591 be equal are still always repleaceable for one another with no
1592 additional changes. Note that this is not the same as saying that they
1593 are definitionally equal, since as we saw extensionally equal functions,
1594 while satisfying the above requirement, are not definitionally equal.
1596 Towards this goal we introduce two equality constructs in
1597 $\myprop$---the fact that they are in $\myprop$ indicates that they
1598 indeed have no computational content. The first construct, $\myarg
1599 \myeq \myarg$, relates types, the second,
1600 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values. The
1601 value-level equality is different from our old propositional equality:
1602 instead of ranging over only one type, we might form equalities between
1603 values of different types---the usefulness of this construct will be
1604 clear soon. In the literature this equality is known as `heterogeneous'
1605 or `John Major', since
1608 John Major's `classless society' widened people's aspirations to
1609 equality, but also the gap between rich and poor. After all, aspiring
1610 to be equal to others than oneself is the politics of envy. In much
1611 the same way, forms equations between members of any type, but they
1612 cannot be treated as equals (ie substituted) unless they are of the
1613 same type. Just as before, each thing is only equal to
1614 itself. \citep{McBride1999}.
1617 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
1618 transport values between equal types; and $\myfun{coh}$ (`coherence')
1619 guarantees that $\myfun{coe}$ respects the value-level equality, or in
1620 other words that it really has no computational component: if we
1621 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
1622 will still be the same.
1624 Before introducing the core ideas that make OTT work, let us distinguish
1625 between \emph{canonical} and \emph{neutral} types. Canonical types are
1626 those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
1627 and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$). Neutral
1628 types are those formed by
1629 $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
1630 Correspondingly, canonical terms are those inhabiting canonical types
1631 ($\mytt$, $\mytrue$, $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$,
1632 ...), and neutral terms those formed by eliminators\footnote{Using the
1633 terminology from section \ref{sec:types}, we'd say that canonical
1634 terms are in \emph{weak head normal form}.}. In the current system
1635 (and hopefully in well-behaved systems), all closed terms reduce to a
1636 canonical term, and all canonical types are inhabited by canonical
1639 \subsubsection{Type equality, and coercions}
1641 The plan is to decompose type-level equalities between canonical types
1642 into decodable propositions containing equalities regarding the
1643 subterms, and to use coerce recursively on the subterms using the
1644 generated equalities. This interplay between type equalities and
1645 \myfun{coe} ensures that invocations of $\myfun{coe}$ will vanish when
1646 we have evidence of the structural equality of the types we are
1647 transporting terms across. If the type is neutral, the equality won't
1648 reduce and thus $\myfun{coe}$ won't reduce either. If we come an
1649 equality between different canonical types, then we reduce the equality
1650 to bottom, making sure that no such proof can exist, and providing an
1651 `escape hatch' in $\myfun{coe}$.
1655 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1657 \begin{array}{c@{\ }c@{\ }c@{\ }l}
1658 \myempty & \myeq & \myempty & \myred \mytop \\
1659 \myunit & \myeq & \myunit & \myred \mytop \\
1660 \mybool & \myeq & \mybool & \myred \mytop \\
1661 \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
1663 \myind{2} \mytya_1 \myeq \mytyb_1 \myand
1664 \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]}
1666 \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1667 \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1668 \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1673 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
1675 \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
1676 \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
1677 \mycoe & \myunit & \myunit & \myse{Q} & \mytt & \myred & \mytt \\
1678 \mycoe & \mybool & \mybool & \myse{Q} & \mytrue & \myred & \mytrue \\
1679 \mycoe & \mybool & \mybool & \myse{Q} & \myfalse & \myred & \myfalse \\
1680 \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1681 (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1682 \mytmt_1 & \myred & \\
1684 \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
1685 \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
1686 & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
1687 & \myb{Q_A} & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
1688 & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
1689 & \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}}} \\
1690 & \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}} \\
1691 \mysyn{in} & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
1694 \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1695 (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1699 \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1700 (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1704 \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred & \\
1706 \myind{2}\myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1711 \caption{Reducing type equalities, and using them when
1712 $\myfun{coe}$rcing.}
1716 Figure \ref{fig:eqred} illustrates this idea in practice. For ground
1717 types, the proof is the trivial element, and \myfun{coe} is the
1718 identity. For the three type binders, things are similar but subtly
1719 different---the choices we make in the type equality are dictated by
1720 the desire of writing the $\myfun{coe}$ in a natural way.
1722 $\myprod$ is the easiest case: we decompose the proof into proofs that
1723 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
1724 proof that given equal values in the first element, the types of the
1725 second elements are equal too
1726 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
1727 \myimpl \mytyb_1 \myeq \mytyb_2}$)\footnote{We are using $\myimpl$ to
1728 indicate a $\forall$ where we discard the first value. We write
1729 $\mytyb_1[\myb{x_1}]$ to indicate that the $\myb{x_1}$ in $\mytyb_1$
1730 is re-bound to the $\myb{x_1}$ quantified by the $\forall$, and
1731 similarly for $\myb{x_2}$ and $\mytyb_2$.}. This also explains the
1732 need for heterogeneous equality, since in the second proof it would be
1733 awkward to express the fact that $\myb{A_1}$ is the same as $\myb{A_2}$.
1734 In the respective $\myfun{coe}$ case, since the types are canonical, we
1735 know at this point that the proof of equality is a pair of the shape
1736 described above. Thus, we can immediately coerce the first element of
1737 the pair using the first element of the proof, and then instantiate the
1738 second element with the two first elements and a proof by coherence of
1739 their equality, since we know that the types are equal. The cases for
1740 the other binders are omitted for brevity, but they follow the same
1743 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
1745 It is important to notice that in the reduction rules for $\myfun{coe}$
1746 are never obstructed by the proofs: with the exception of comparisons
1747 between different canonical types we never pattern match on the pairs,
1748 but always look at the projections. This means that, as long as we are
1749 consistent, and thus as long as we don't have $\mybot$-inducing proofs,
1750 we can add propositional axioms for equality and $\myfun{coe}$ will
1751 still compute. Thus, we can take $\myfun{coh}$ as axiomatic, and we can
1752 add back familiar useful equality rules:
1754 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1755 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1756 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\myb{x}}{\myb{\mytya}}{\myb{x}}{\myb{\mytya}}}}$}
1761 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1762 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
1763 \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}}}}}$}
1767 $\myrefl$ is the equivalent of the reflexivity rule in propositional
1768 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
1769 abstracting over a value we can substitute equal for equal---this lets
1770 us recover $\myfun{subst}$. Note that while we need to provide ad-hoc
1771 rules in the restricted, non-hierarchical theory that we have, if our
1772 theory supports abstraction over $\mytyp$s we can easily add these
1773 axioms as abstracted variables.
1775 \subsubsection{Value-level equality}
1777 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1779 \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
1780 (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
1781 (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty&) & \myred \mytop \\
1782 (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
1783 (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
1784 (&\mytmt_1 & : & \mybool&) & \myeq & (&\mytmt_1 & : & \mybool&) & \myred \mybot \\
1785 (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1786 & \multicolumn{11}{@{}l}{
1787 \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
1788 \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}}}
1790 (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1791 & \multicolumn{11}{@{}l}{
1792 \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
1793 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
1794 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
1797 (&\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 \\
1798 (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \\
1799 & \multicolumn{11}{@{}l}{
1800 \myind{2} \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
1806 As with type-level equality, we want value-level equality to reduce
1807 based on the structure of the compared terms.
1809 \subsection{Proof irrelevance}
1811 % \section{Augmenting ITT}
1812 % \label{sec:practical}
1814 % \subsection{A more liberal hierarchy}
1816 % \subsection{Type inference}
1818 % \subsubsection{Bidirectional type checking}
1820 % \subsubsection{Pattern unification}
1822 % \subsection{Pattern matching and explicit fixpoints}
1824 % \subsection{Induction-recursion}
1826 % \subsection{Coinduction}
1828 % \subsection{Dealing with partiality}
1830 % \subsection{Type holes}
1832 \section{\mykant : the theory}
1833 \label{sec:kant-theory}
1835 \mykant\ is an interactive theorem prover developed as part of this thesis.
1836 The plan is to present a core language which would be capable of serving as
1837 the basis for a more featureful system, while still presenting interesting
1838 features and more importantly observational equality.
1840 The author learnt the hard way the implementations challenges for such a
1841 project, and while there is a solid and working base to work on, observational
1842 equality is not currently implemented. However, a detailed plan on how to add
1843 it this functionality is provided, and should not prove to be too much work.
1845 The features currently implemented in \mykant\ are:
1848 \item[Full dependent types] As we would expect, we have dependent a system
1849 which is as expressive as the `best' corner in the lambda cube described in
1850 section \ref{sec:itt}.
1852 \item[Implicit, cumulative universe hierarchy] The user does not need to
1853 specify universe level explicitly, and universes are \emph{cumulative}.
1855 \item[User defined data types and records] Instead of forcing the user to
1856 choose from a restricted toolbox, we let her define inductive data types,
1857 with associated primitive recursion operators; or records, with associated
1858 projections for each field.
1860 \item[Bidirectional type checking] While no `fancy' inference via unification
1861 is present, we take advantage of an type synthesis system in the style of
1862 \cite{Pierce2000}, extending the concept for user defined data types.
1864 \item[Type holes] When building up programs interactively, it is useful to
1865 leave parts unfinished while exploring the current context. This is what
1869 The planned features are:
1872 \item[Observational equality] As described in section \ref{sec:ott} but
1873 extended to work with the type hierarchy and to admit equality between
1874 arbitrary data types.
1876 \item[Coinductive data] ...
1879 We will analyse the features one by one, along with motivations and tradeoffs
1880 for the design decisions made.
1882 \subsection{Bidirectional type checking}
1884 We start by describing bidirectional type checking since it calls for fairly
1885 different typing rules that what we have seen up to now. The idea is to have
1886 two kind of terms: terms for which a type can always be inferred, and terms
1887 that need to be checked against a type. A nice observation is that this
1888 duality runs through the semantics of the terms: data destructors (function
1889 application, record projections, primitive re cursors) \emph{infer} types,
1890 while data constructors (abstractions, record/data types data constructors)
1891 need to be checked. In the literature these terms are respectively known as
1893 To introduce the concept and notation, we will revisit the STLC in a
1894 bidirectional style. The presentation follows \cite{Loh2010}.
1896 % TODO do this --- is it even necessary
1900 \subsection{Base terms and types}
1902 Let us begin by describing the primitives available without the user
1903 defining any data types, and without equality. The way we handle
1904 variables and substitution is left unspecified, and explained in section
1905 \ref{sec:term-repr}, along with other implementation issues. We are
1906 also going to give an account of the implicit type hierarchy separately
1907 in section \ref{sec:term-hierarchy}, so as not to clutter derivation
1908 rules too much, and just treat types as impredicative for the time
1913 \begin{array}{r@{\ }c@{\ }l}
1914 \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
1915 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1916 \myabs{\myb{x}}{\mytmsyn} \mysynsep
1917 (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
1918 (\myann{\mytmsyn}{\mytmsyn}) \\
1919 \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
1924 The syntax for our calculus includes just two basic constructs:
1925 abstractions and $\mytyp$s. Everything else will be provided by
1926 user-definable constructs. Since we let the user define values, we will
1927 need a context capable of carrying the body of variables along with
1928 their type. Bound names and defined names are treated separately in the
1929 syntax, and while both can be associated to a type in the context, only
1930 defined names can be associated with a body:
1932 \mydesc{context validity:}{\myvalid{\myctx}}{
1933 \begin{tabular}{ccc}
1934 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1935 \UnaryInfC{$\myvalid{\myemptyctx}$}
1938 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1939 \AxiomC{$\mynamesyn \not\in \myctx$}
1940 \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
1943 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1944 \AxiomC{$\myfun{f} \not\in \myctx$}
1945 \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
1950 Now we can present the reduction rules, which are unsurprising. We have
1951 the usual function application ($\beta$-reduction), but also a rule to
1952 replace names with their bodies ($\delta$-reduction), and one to discard
1953 type annotations. For this reason reduction is done in-context, as
1954 opposed to what we have seen in the past:
1956 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
1957 \begin{tabular}{ccc}
1958 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1959 \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
1960 \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
1963 \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
1964 \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
1967 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1968 \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
1973 We can now give types to our terms. The type of names, both defined and
1974 abstract, is inferred. The type of applications is inferred too,
1975 propagating types down the applied term. Abstractions are checked.
1976 Finally, we have a rule to check the type of an inferrable term. We
1977 defer the question of term equality (which is needed for type checking)
1978 to section \label{sec:kant-irr}.
1980 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
1981 \begin{tabular}{ccc}
1982 \AxiomC{$\myse{name} : A \in \myctx$}
1983 \UnaryInfC{$\myinf{\myse{name}}{A}$}
1986 \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
1987 \UnaryInfC{$\myinf{\myfun{f}}{A}$}
1990 \AxiomC{$\myinf{\mytmt}{\mytya}$}
1991 \UnaryInfC{$\mychk{\myann{\mytmt}{\mytya}}{\mytya}$}
1996 \begin{tabular}{ccc}
1997 \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1998 \AxiomC{$\mychk{\mytmn}{\mytya}$}
1999 \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
2004 \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
2005 \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2010 \subsection{Elaboration}
2012 As we mentioned, $\mykant$\ allows the user to define not only values
2013 but also custom data types and records. \emph{Elaboration} consists of
2014 turning these declarations into workable syntax, types, and reduction
2015 rules. The treatment of custom types in $\mykant$\ is heavily inspired
2016 by McBride and McKinna early work on Epigram \citep{McBride2004},
2017 although with some differences.
2019 \subsubsection{Term vectors, telescopes, and assorted notation}
2021 We use a vector notation to refer to a series of term applied to
2022 another, for example $\mytyc{D} \myappsp \vec{A}$ is a shorthand for
2023 $\mytyc{D} \myappsp \mytya_1 \cdots \mytya_n$, for some $n$. $n$ is
2024 consistently used to refer to the length of such vectors, and $i$ to
2025 refer to an index in such vectors. We also often need to `build up'
2026 terms vectors, in which case we use $\myemptyctx$ for an empty vector
2027 and add elements to an existing vector with $\myarg ; \myarg$, similarly
2028 to what we do for context.
2030 To present the elaboration and operations on user defined data types, we
2031 frequently make use what de Bruijn called \emph{telescopes}
2032 \citep{Bruijn91}, a construct that will prove useful when dealing with
2033 the types of type and data constructors. A telescope is a series of
2034 nested typed bindings, such as $(\myb{x} {:} \mynat); (\myb{p} :
2035 \myapp{\myfun{even}}{\myb{x}})$. Consistently with the notation for
2036 contexts and term vectors, we use $\myemptyctx$ to denote an empty
2037 telescope and $\myarg ; \myarg$ to add a new binding to an existing
2040 We refer to telescopes with $\mytele$, $\mytele'$, $\mytele_i$, etc. If
2041 $\mytele$ refers to a telescope, $\mytelee$ refers to the term vector
2042 made up of all the variables bound by $\mytele$. $\mytele \myarr
2043 \mytya$ refers to the type made by turning the telescope into a series
2044 of $\myarr$. Returning to the examples above, we have that
2046 (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2047 (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2050 We make use of various operations to manipulate telescopes:
2052 \item $\myhead(\mytele)$ refers to the first type appearing in
2053 $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2054 \myapp{\myfun{even}}{\myb{x}})) = \mynat$. Similarly,
2055 $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2057 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2058 first $i$ elements of $\mytele$: $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2059 \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$
2060 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2061 terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2062 \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2063 \myapp{\myfun{even}}{42})$.
2066 \subsubsection{Declarations syntax}
2070 \begin{array}{r@{\ }c@{\ }l}
2071 \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2072 & | & \mypost{\myb{x}}{\mytmsyn} \\
2073 & | & \myadt{\mytyc{D}}{\mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2074 & | & \myreco{\mytyc{D}}{\mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2076 \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2077 \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2082 In \mykant\ we have four kind of declarations:
2085 \item[Defined value] A variable, together with a type and a body.
2086 \item[Abstract variable] An abstract variable, with a type but no body.
2087 \item[Inductive data] A datatype, with a type constructor and various data
2088 constructors---somewhat similar to what we find in Haskell. A primitive
2089 recursor (or `destructor') will be generated automatically.
2090 \item[Record] A record, which consists of one data constructor and various
2091 fields, with no recursive occurrences.
2094 Elaborating defined variables consists of type checking body against the
2095 given type, and updating the context to contain the new binding.
2096 Elaborating abstract variables and abstract variables consists of type
2097 checking the type, and updating the context with a new typed variable:
2099 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2101 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2102 \AxiomC{$\myfun{f} \not\in \myctx$}
2104 $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \ \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2108 \AxiomC{$\myjud{\mytya}{\mytyp}$}
2109 \AxiomC{$\myfun{f} \not\in \myctx$}
2112 \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2113 \ \ \myelabf\ \ \myctx; \myfun{f} : \mytya
2120 \subsubsection{User defined types}
2121 \label{sec:user-type}
2123 Elaborating user defined types is the real effort. First, let's explain
2124 what we can defined, with some examples.
2127 \item[Natural numbers] To define natural numbers, we create a data type
2128 with two constructors: one with zero arguments ($\mydc{zero}$) and one
2129 with one recursive argument ($\mydc{suc}$):
2132 \myadt{\mynat}{ }{ }{
2133 \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2137 This is very similar to what we would write in Haskell:
2138 {\small\[\text{\texttt{data Nat = Zero | Suc Nat}}\]}
2139 Once the data type is defined, $\mykant$\ will generate syntactic
2140 constructs for the type and data constructors, so that we will have
2143 \begin{tabular}{ccc}
2144 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2145 \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2148 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2149 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2152 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2153 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2157 While in Haskell (or indeed in Agda or Coq) data constructors are
2158 treated the same way as functions, in $\mykant$\ they are syntax, so
2159 for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will be a
2160 syntax error. This is necessary so that we can easily infer the type
2161 of polymorphic data constructors, as we will see later.
2163 Moreover, each data constructor is prefixed by the type constructor
2164 name, since we need to retrieve the type constructor of a data
2165 constructor when type checking. This measure aids in the presentation
2166 of various features but it is not needed in the implementation, where
2167 we can have a dictionary to lookup the type constructor corresponding
2168 to each data constructor. When using data constructors in examples I
2169 will omit the type constructor prefix for brevity.
2171 Along with user defined constructors, $\mykant$\ automatically
2172 generates an \emph{eliminator}, or \emph{destructor}, to compute with
2173 natural numbers: If we have $\mytmt : \mynat$, we can destruct
2174 $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2177 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2179 \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2181 \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}}
2185 $\mynat.\myfun{elim}$ corresponds to the induction principle for
2186 natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2187 know that predicate holds for the base case
2188 ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2189 ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2190 \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2191 holds for any number. As with the data constructors, we require the
2192 eliminator to be applied to the `destructed' element.
2194 While the induction principle is usually seen as a mean to prove
2195 properties about numbers, in the intuitionistic setting it is also a
2196 mean to compute. In this specific case we will $\mynat.\myfun{elim}$
2197 will return the base case if the provided number is $\mydc{zero}$, and
2198 recursively apply the inductive step if the number is a
2201 \begin{array}{@{}l@{}l}
2202 \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2203 \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})
2206 The Haskell equivalent would be
2209 \text{\texttt{elim :: Nat -> a -> (Nat -> a -> a) -> a}}\\
2210 \text{\texttt{elim Zero pz ps = pz}}\\
2211 \text{\texttt{elim (Suc n) pz ps = ps n (elim n pz ps)}}
2214 Which buys us the computational behaviour, but not the reasoning power.
2215 % TODO maybe more examples, e.g. Haskell eliminator and fibonacci
2217 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2218 lists are too similar to natural numbers to be interesting.
2221 \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2222 \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2226 Now the purpose of constructors as syntax can be explained: what would
2227 the type of $\mydc{leaf}$ be? If we were to treat it as a `normal'
2228 term, we would have to specify the type parameter of the tree each
2229 time the constructor is applied:
2231 \begin{array}{@{}l@{\ }l}
2232 \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2233 \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2236 The problem with this approach is that creating terms is incredibly
2237 verbose and dull, since we would need to specify the type parameters
2238 each time. For example if we wished to create a $\mytree \myappsp
2239 \mynat$ with two nodes and three leaves, we would have to write
2241 \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)
2243 The redundancy of $\mynat$s is quite irritating. Instead, if we treat
2244 constructors as syntactic elements, we can `extract' the type of the
2245 parameter from the type that the term gets checked against, much like
2246 we get the type of abstraction arguments:
2250 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2251 \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2254 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2255 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2256 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2257 \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2261 Which enables us to write, much more concisely
2263 \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}
2265 We gain an annotation, but we lose the myriad of types applied to the
2266 constructors. Conversely, with the eliminator for $\mytree$, we can
2267 infer the type of the arguments given the type of the destructed:
2270 \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2272 \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2274 (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2275 \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2276 ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2277 \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2278 \myapp{\myb{P}}{\mytmt}
2283 As expected, the eliminator embodies structural induction on trees.
2285 \item[Empty type] We have presented types that have at least one
2286 constructors, but nothing prevents us from defining types with
2287 \emph{no} constructors:
2289 \myadt{\mytyc{Empty}}{ }{ }{ }
2291 What shall the `induction principle' on $\mytyc{Empty}$ be? Does it
2292 even make sense to talk about induction on $\mytyc{Empty}$?
2293 $\mykant$\ does not care, and generates an eliminator with no `cases',
2294 and thus corresponding to the $\myfun{absurd}$ that we know and love:
2297 \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2298 \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2301 \item[Ordered lists] Up to this point, the examples shown are nothing
2302 new to the \{Haskell, SML, OCaml, functional\} programmer. However
2303 dependent types let us express much more than
2306 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
2307 us another way to define types: $\mysyn{record}$. A record is a
2308 datatype with one constructor and `projections' to extract specific
2309 fields of the said constructor.
2311 For example, we can recover dependent products:
2314 \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}}}
2317 Here $\myfst$ and $\mysnd$ are the projections, with their respective
2318 types. Note that each field can refer to the preceding fields. A
2319 constructor will be automatically generated, under the name of
2320 $\mytyc{Prod}.\mydc{constr}$. Dually to data types, we will omit the
2321 type constructor prefix for record projections.
2323 Following the bidirectionality of the system, we have that projections
2324 (the destructors of the record) infer the type, while the constructor
2329 \AxiomC{$\mychk{\mytmm}{\mytya}$}
2330 \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
2331 \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2333 \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
2336 \AxiomC{$\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2337 \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
2339 \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
2343 What we have is equivalent to ITT's dependent products.
2347 \begin{subfigure}[b]{\textwidth}
2353 \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2358 \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
2361 \begin{array}{r@{\ }l}
2362 & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
2365 \begin{array}{r@{\ }c@{\ }l}
2366 \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
2367 \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
2373 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2378 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2379 \mytyc{D} \not\in \myctx \\
2380 \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
2381 \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
2385 \begin{array}{r@{\ }c@{\ }l}
2386 \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
2387 & & \vspace{-0.2cm} \\
2388 & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
2390 \begin{array}{@{}r@{\ }l l}
2391 \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
2392 & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
2396 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
2397 \end{array} \right \}
2398 & \textbf{methods} \\
2399 & \myapp{\myb{P}}{\myb{x}} &
2403 \DisplayProof \\ \vspace{0.2cm}\ \\
2405 \begin{array}{@{}l l@{\ } l@{} r c l}
2406 \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2407 & \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) \\
2408 & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
2414 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2416 $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
2417 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2418 \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
2420 \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)}
2422 \DisplayProof \\ \vspace{0.2cm}\ \\
2424 \begin{array}{@{}l l@{\ } l@{} r c l}
2425 \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2426 & \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) \\
2427 & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
2433 \begin{subfigure}[b]{\textwidth}
2434 \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
2437 \begin{array}{r@{\ }c@{\ }l}
2438 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2441 \begin{array}{r@{\ }c@{\ }l}
2442 \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 \\
2449 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2453 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2454 \mytyc{D} \not\in \myctx \\
2455 \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
2459 \begin{array}{r@{\ }c@{\ }l}
2460 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2461 & & \vspace{-0.2cm} \\
2462 & \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}; \\
2463 & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
2469 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2471 $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
2472 \AxiomC{$\mytyc{D} \in \myctx$}
2473 \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
2478 \caption{Elaboration for data types and records.}
2482 Following the intuition given by the examples, the mechanised
2483 elaboration is presented in figure \ref{fig:elab}, which is essentially
2484 a modification of figure 9 of \citep{McBride2004}\footnote{However, our
2485 datatypes do not have indices, we do bidirectional typechecking by
2486 treating constructors/destructors as syntactic constructs, and we have
2489 In data types declarations we allow recursive occurrences as long as
2490 they are \emph{strictly positive}, employing a syntactic check to make
2491 sure that this is the case. See \cite{Dybjer1991} for a more formal
2492 treatment of inductive definitions in ITT.
2494 For what concerns records, recursive occurrences are disallowed. The
2495 reason for this choice is answered by the reason for the choice of
2496 having records at all: we need records to give the user types with
2497 $\eta$-laws for equality, as we saw in section % TODO add section
2498 and in the treatment of OTT in section \ref{sec:ott}. If we tried to
2499 $\eta$-expand recursive data types, we would expand forever.
2501 To implement bidirectional type checking for constructors and
2502 destructors, we store their types in full in the context, and then
2503 instantiate when due:
2505 \mydesc{typing:}{ }{
2508 \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
2509 \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
2510 \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
2511 \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
2512 \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \
2513 (1 \le i \le \mytele'')
2516 \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
2521 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2522 \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
2523 \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
2524 \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
2525 \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
2526 \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
2527 \myse{F})(\vec{A};\mytmt)}$}
2531 \subsubsection{Why user defined types? Why eliminators?}
2533 % TODO reference levitated theories, indexed containers
2537 \subsection{Cumulative hierarchy and typical ambiguity}
2538 \label{sec:term-hierarchy}
2540 A type hierarchy as presented in section \label{sec:itt} is a
2541 considerable burden on the user, on various levels. Consider for
2542 example how we recovered disjunctions in section \ref{sec:disju}: we
2543 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
2544 What if we wanted to form a disjunction containing two $\mytyp_0$, or
2545 $\mytyp_{42}$? Our definition would fail us, since $\mytyp_0 :
2548 One way to solve this issue is a \emph{cumulative} hierarchy, where
2549 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$. This way we retain
2550 consistency, while allowing for `large' definitions that work on small
2551 types too. For example we might define our disjunction to be
2553 \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
2555 And hope that $\mytyp_{100}$ will be large enough to fit all the types
2556 that we want to use with our disjunction. However, there are two
2557 problems with this. First, there is the obvious clumsyness of having to
2558 manually specify the size of types. More importantly, if we want to use
2559 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
2560 make sure that those allow for types at least as large as
2563 A better option is to employ a mechanised version of what Russell called
2564 \emph{typical ambiguity}: we let the user live under the illusion that
2565 $\mytyp : \mytyp$, but check that the statements about types are
2566 consistent behind the hood. $\mykant$\ implements this following the
2567 lines of \cite{Huet1988}. See also \citep{Harper1991} for a published
2568 reference, although describing a more complex system allowing for both
2569 explicit and explicit hierarchy at the same time.
2571 We define a partial ordering on the levels, with both weak ($\le$) and
2572 strong ($<$) constraints---the laws governing them being the same as the
2573 ones governing $<$ and $\le$ for the natural numbers. Each occurrence
2574 of $\mytyp$ is decorated with a unique reference, and we keep a set of
2575 constraints and add new constraints as we type check, generating new
2576 references when needed.
2578 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
2579 denotes the unique reference assigned to that term, we will generate a
2580 new fresh reference $\mytyp\, r_2$, and add the constraint $r_1 < r_2$
2581 to the set. When type checking $\myctx \vdash
2582 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
2583 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
2584 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
2587 If at any point the constraint set becomes inconsistent, type checking
2588 fails. Moreover, when comparing two $\mytyp$ terms we equate their
2589 respective references with two $\le$ constraints---the details are
2590 explained in section \ref{sec:hier-impl}.
2592 Another more flexible but also more verbose alternative is the one
2593 chosen by Agda, where levels can be quantified so that the relationship
2594 between arguments and result in type formers can be explicitly
2597 \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}
2599 Inference algorithms to automatically derive this kind of relationship
2600 are currently subject of research. We chose less flexible but more
2601 concise way, since it is easier to implement and better understood.
2603 \subsection{Observational equality, \mykant\ style}
2605 There are two correlated differences between $\mykant$\ and the theory
2606 used to present OTT. The first is that in $\mykant$ we have a type
2607 hierarchy, which lets us, for example, abstract over types. The second
2608 is that we let the user define inductive types.
2610 Reconciling propositions for OTT and a hierarchy had already been
2611 investigated by Conor McBride\footnote{See
2612 \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.}, and we
2613 follow his footsteps. Most of the work, as an extension of elaboration,
2614 is to generate reduction rules and coercions.
2616 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
2618 Before defining $\myprop$, we define some basic types inside $\mykant$,
2619 as the target for the $\myprop$ decoder:
2624 \myadt{\mytyc{Empty}}{}{ }{ } \\
2625 \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
2626 \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
2629 \myreco{\mytyc{Unit}}{}{}{ } \\ \ \\
2631 \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
2635 When using $\mytyc{Prod}$, we shall use $\myprod$ to define `nested'
2636 products, and $\myproj{n}$ to project elements from them, so that
2640 \mytya \myprod \mytyb = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myunit) \\
2641 \mytya \myprod \mytyb \myprod \myse{C} = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp (\mytyc{Prod} \myappsp \mytyc \myappsp \myunit)) \\
2643 \myproj{1} : \mytyc{Prod} \myappsp \mytya \myappsp \mytyb \myarr \mytya \\
2644 \myproj{2} : \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myse{C}) \myarr \mytyb \\
2649 And so on, so that $\myproj{n}$ will work with all products with at
2650 least than $n$ elements. Then we can define propositions, and decoding:
2654 \begin{array}{r@{\ }c@{\ }l}
2655 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
2656 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
2661 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2664 \begin{array}{l@{\ }c@{\ }l}
2665 \myprdec{\mybot} & \myred & \myempty \\
2666 \myprdec{\mytop} & \myred & \myunit
2671 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2672 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2673 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2674 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2680 \subsubsection{Why $\myprop$?}
2682 It is worth to ask if $\myprop$ is needed at all. It is perfectly
2683 possible to have the type checker identify propositional types
2684 automatically, and in fact that is what The author initially planned to
2685 identify the propositional fragment internally \cite{Jacobs1994}.
2689 \subsubsection{OTT constructs}
2691 Before presenting the direction that $\mykant$\ takes, let's consider
2692 some examples of use-defined data types, and the result we would expect,
2693 given what we already know about OTT, assuming the same propositional
2698 \item[Product types] Let's consider first the already mentioned
2699 dependent product, using the alternate name $\mysigma$\footnote{For
2700 extra confusion, `dependent products' are often called `dependent
2701 sums' in the literature, referring to the interpretation that
2702 identifies the first element as a `tag' deciding the type of the
2703 second element, which lets us recover sum types (disjuctions), as we
2704 saw in section \ref{sec:user-type}. Thus, $\mysigma$.} to
2705 avoid confusion with the $\mytyc{Prod}$ in the prelude: {\small\[
2707 \myreco{\mysigma}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
2709 \]} Let's start with type-level equality. The result we want is
2712 \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\
2713 \myind{2} \mytya_1 \myeq \mytya_2 \myand \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \myapp{\mytyb_1}{\myb{x_1}} \myeq \myapp{\mytyb_2}{\myb{x_2}}}
2715 \]} The difference here is that in the original presentation of OTT
2716 the type binders are explicit, while here $\mytyb_1$ and $\mytyb_2$
2717 functions returning types. We can do this thanks to the type
2718 hierarchy, and this hints at the fact that heterogeneous equality will
2719 have to allow $\mytyp$ `to the right of the colon', and in fact this
2720 provides the solution to simplify the equality above.
2722 If we take, just like we saw previously in OTT
2725 \myjm{\myse{f}_1}{\myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}}{\myse{f}_2}{\myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}} \myred \\
2726 \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
2727 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
2728 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
2731 \]} Then we can simply take
2734 \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\ \myind{2} \mytya_1 \myeq \mytya_2 \myand \myjm{\mytyb_1}{\mytya_1 \myarr \mytyp}{\mytyb_2}{\mytya_2 \myarr \mytyp}
2736 \]} Which will reduce to precisely what we desire. For what
2737 concerns coercions and quotation, things stay the same (apart from the
2738 fact that we apply to the second argument instead of substituting).
2739 We can recognise records such as $\mysigma$ as such and employ
2740 projections in value equality, coercions, and quotation; as to not
2741 impede progress if not necessary.
2743 \item[Lists] Now for finite lists, which will give us a taste for data
2747 \myadt{\mylist}{\myappsp (\myb{A} {:} \mytyp)}{ }{\mydc{nil} \mydcsep \mydc{cons} \myappsp \myb{A} \myappsp (\myapp{\mylist}{\myb{A}})}
2750 Type equality is simple---we only need to compare the parameter:
2752 \mylist \myappsp \mytya_1 \myeq \mylist \myappsp \mytya_2 \myred \mytya_1 \myeq \mytya_2
2753 \]} For coercions, we transport based on the constructor, recycling
2754 the proof for the inductive occurrence: {\small\[
2755 \begin{array}{@{}l@{\ }c@{\ }l}
2756 \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mydc{nil} & \myred & \mydc{nil} \\
2757 \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp (\mydc{cons} \myappsp \mytmm \myappsp \mytmn) & \myred & \\
2758 \multicolumn{3}{l}{\myind{2} \mydc{cons} \myappsp (\mycoe \myappsp \mytya_1 \myappsp \mytya_2 \myappsp \myse{Q} \myappsp \mytmm) \myappsp (\mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mytmn)}
2760 \]} Value equality is unsurprising---we match the constructors, and
2761 return bottom for mismatches. However, we also need to equate the
2762 parameter in $\mydc{nil}$: {\small\[
2763 \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
2764 (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mytya_1 \myeq \mytya_2 \\
2765 (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \\
2766 & \multicolumn{11}{@{}l}{ \myind{2}
2767 \myjm{\mytmm_1}{\mytya_1}{\mytmm_2}{\mytya_2} \myand \myjm{\mytmn_1}{\myapp{\mylist}{\mytya_1}}{\mytmn_2}{\myapp{\mylist}{\mytya_2}}
2769 (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot \\
2770 (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot
2784 \begin{array}{r@{\ }c@{\ }l}
2785 \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
2786 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2787 \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2793 \mydesc{typing:}{\myctx \vdash \myprsyn \myred \myprsyn}{
2796 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2797 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2798 \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
2801 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2802 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2803 \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
2807 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
2810 \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
2811 \UnaryInfC{$\myjud{\mytop}{\myprop}$}
2813 \UnaryInfC{$\myjud{\mybot}{\myprop}$}
2816 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
2817 \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
2818 \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
2820 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
2829 \phantom{\myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}}} \\
2830 \myjud{\myse{A}}{\mytyp}\hspace{0.8cm}
2831 \myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}
2834 \UnaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
2839 \myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}} \\
2840 \myjud{\myse{B}}{\mytyp} \hspace{0.8cm} \myjud{\mytmn}{\myse{B}}
2843 \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
2847 % TODO equality for decodings
2848 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
2851 \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
2858 \begin{array}{@{}r@{\ }l}
2860 \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp} \myred \\
2861 & \myind{2} \mytya_2 \myeq \mytya_1 \myand \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
2862 \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1 \myeq \mytyb_2
2872 \begin{array}{@{}r@{\ }l}
2874 \myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}} \myred \\
2875 & \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
2876 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
2877 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
2886 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2888 \begin{array}{r@{\ }l}
2890 \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp} \myred \\
2891 & \myind{2} \mybigand_{i = 1}^n (\myjm{\mytya_n}{\myhead(\mytele(A_1 \cdots A_{i-1}))}{\mytyb_i}{\myhead(\mytele(B_1 \cdots B_{i-1}))})
2900 \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
2901 \mytyc{D}.\mydc{c} : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \\
2902 \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
2903 \mytele_B = (\mytele;\mytele')\vec{B}
2907 \begin{array}{@{}l@{\ }l}
2908 \myctx \vdash & \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \\
2909 & \myind{2} \mybigand_{i=1}^n(\myjm{\mytmm_i}{\myhead(\mytele_A (\mytya_i \cdots \mytya_{i-1}))}{\mytmn_i}{\myhead(\mytele_B (\mytyb_i \cdots \mytyb_{i-1}))})
2916 \AxiomC{$\mydataty(\mytyc{D}, \myctx)$}
2918 \myctx \vdash \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c'} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \mybot
2926 \myisreco(\mytyc{D}, \myctx)\hspace{0.8cm}
2927 \mytyc{D}.\myfun{f}_i : \mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i \in \myctx\\
2931 \begin{array}{@{}l@{\ }l}
2932 \myctx \vdash & \myjm{\myse{l}}{\mytyc{D} \myappsp \vec{A}}{\myse{r}}{\mytyc{D} \myappsp \vec{B}} \myred \\ & \myind{2} \mybigand_{i=1}^n(\myjm{\mytyc{D}.\myfun{f}_1 \myappsp \myse{l}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytya};\myse{l})}{\mytyc{D}.\myfun{f}_i \myappsp \myse{r}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytyb};\myse{r})})
2939 \UnaryInfC{$\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb} \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
2942 \caption{Equality reduction for $\mykant$.}
2943 \label{fig:kant-eq-red}
2946 \subsubsection{$\myprop$ and the hierarchy}
2948 Where is $\myprop$ placed in the $\mytyp$ hierarchy? At each universe
2949 level, we will have that
2951 \subsubsection{Quotation and irrelevance}
2956 \section{\mykant : The practice}
2957 \label{sec:kant-practice}
2959 The codebase consists of around 2500 lines of Haskell, as reported by
2960 the \texttt{cloc} utility. The high level design is inspired by Conor
2961 McBride's work on various incarnations of Epigram, and specifically by
2962 the first version as described \citep{McBride2004} and the codebase for
2963 the new version \footnote{Available intermittently as a \texttt{darcs}
2964 repository at \url{http://sneezy.cs.nott.ac.uk/darcs/Pig09}.}. In
2965 many ways \mykant\ is something in between the first and second version
2968 The interaction happens in a read-eval-print loop (REPL). The REPL is a
2969 available both as a commandline application and in a web interface,
2970 which is available at \url{kant.mazzo.li} and presents itself as in
2971 figure \ref{fig:kant-web}.
2975 \includegraphics[scale=1.0]{kant-web.png}
2977 \caption{The \mykant\ web prompt.}
2978 \label{fig:kant-web}
2981 The interaction with the user takes place in a loop living in and updating a
2982 context \mykant\ declarations. The user inputs a new declaration that goes
2983 through various stages starts with the user inputing a \mykant\ declaration or
2984 another REPL command, which then goes through various stages that can end up
2985 in a context update, or in failures of various kind. The process is described
2986 diagrammatically in figure \ref{fig:kant-process}:
2989 \item[Parse] In this phase the text input gets converted to a sugared
2990 version of the core language.
2992 \item[Desugar] The sugared declaration is converted to a core term.
2994 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
2995 which is necessary to implement the type hierarchy check.
2997 \item[Elaborate] Convert the declaration to some context item, which might be
2998 a value declaration (type and body) or a data type declaration (constructors
2999 and destructors). This phase works in tandem with \textbf{Typechecking},
3000 which in turns needs to \textbf{Evaluate} terms.
3002 \item[Distill] and report the result. `Distilling' refers to the process of
3003 converting a core term back to a sugared version that the user can
3004 visualise. This can be necessary both to display errors including terms or
3005 to display result of evaluations or type checking that the user has
3008 \item[Pretty print] Format the terms in a nice way, and display the result to
3015 \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
3016 corners, minimum height=2.5em, node distance=0.7cm]
3018 \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
3019 centered, inner sep=0pt, node distance=0.7cm]
3021 \tikzstyle{line} = [draw, -latex']
3023 \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
3024 centered, node distance=1.5cm]
3027 \begin{tikzpicture}[auto]
3028 \node [cloud] (user) {User};
3029 \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
3030 \node [block, below=of parse] (desugar) {Desugar};
3031 \node [block, below=of desugar] (reference) {Reference};
3032 \node [block, below=of reference] (elaborate) {Elaborate};
3033 \node [block, left=of elaborate] (tycheck) {Typecheck};
3034 \node [block, left=of tycheck] (evaluate) {Evaluate};
3035 \node [decision, right=of elaborate] (error) {Error?};
3036 \node [block, right=of parse] (distill) {Distill};
3037 \node [block, right=of desugar] (update) {Update context};
3039 \path [line] (user) -- (parse);
3040 \path [line] (parse) -- (desugar);
3041 \path [line] (desugar) -- (reference);
3042 \path [line] (reference) -- (elaborate);
3043 \path [line] (elaborate) edge[bend right] (tycheck);
3044 \path [line] (tycheck) edge[bend right] (elaborate);
3045 \path [line] (elaborate) -- (error);
3046 \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
3047 \path [line] (error) -- node [near start] {no} (update);
3048 \path [line] (update) -- (distill);
3049 \path [line] (distill) -- (user);
3050 \path [line] (tycheck) edge[bend right] (evaluate);
3051 \path [line] (evaluate) edge[bend right] (tycheck);
3054 \caption{High level overview of the life of a \mykant\ prompt cycle.}
3055 \label{fig:kant-process}
3058 \subsection{Parsing and \texttt{Sugar}}
3060 \subsection{Term representation and context}
3061 \label{sec:term-repr}
3063 \subsection{Type checking}
3065 \subsection{Type hierarchy}
3066 \label{sec:hier-impl}
3068 \subsection{Elaboration}
3070 \section{Evaluation}
3072 \section{Future work}
3074 % TODO coinduction (obscoin, gimenez), pattern unification (miller,
3075 % gundry), partiality monad (NAD)
3079 \section{Notation and syntax}
3081 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
3082 the type of relation being established and the syntactic elements appearing,
3085 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
3086 Typing derivations here.
3089 In the languages presented and Agda code samples I also highlight the syntax,
3090 following a uniform color and font convention:
3093 \begin{tabular}{c | l}
3094 $\mytyc{Sans}$ & Type constructors. \\
3095 $\mydc{sans}$ & Data constructors. \\
3096 % $\myfld{sans}$ & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
3097 $\mysyn{roman}$ & Keywords of the language. \\
3098 $\myfun{roman}$ & Defined values and destructors. \\
3099 $\myb{math}$ & Bound variables.
3103 Moreover, I will from time to time give examples in the Haskell programming
3104 language as defined in \citep{Haskell2010}, which I will typeset in
3105 \texttt{teletype} font. I assume that the reader is already familiar with
3106 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
3108 When presenting grammars, I will use a word in $\mysynel{math}$ font
3109 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate nonterminals. Additionally,
3110 I will use quite flexibly a $\mysynel{math}$ font to indicate a syntactic
3111 element. More specifically, terms are usually indicated by lowercase letters
3112 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase letter (often
3113 $\mytya$, $\mytyb$, or $\mytycc$).
3115 When presenting type derivations, I will often abbreviate and present multiple
3116 conclusions, each on a separate line:
3118 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
3119 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
3121 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
3124 I will often present `definition' in the described calculi and in
3125 $\mykant$\ itself, like so:
3128 \myfun{name} : \mytysyn \\
3129 \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
3132 To define operators, I use a mixfix notation similar
3133 to Agda, where $\myarg$s denote arguments, for example
3136 \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
3137 \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
3143 \subsection{ITT renditions}
3144 \label{app:itt-code}
3146 \subsubsection{Agda}
3147 \label{app:agda-itt}
3149 Note that in what follows rules for `base' types are
3150 universe-polymorphic, to reflect the exposition. Derived definitions,
3151 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
3152 that in the theory presented we don't have universe polymorphism.
3158 data Empty : Set where
3160 absurd : ∀ {a} {A : Set a} → Empty → A
3163 ¬_ : ∀ {a} → (A : Set a) → Set a
3166 record Unit : Set where
3169 record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
3176 data Bool : Set where
3179 if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
3180 if true / _ then x else _ = x
3181 if false / _ then _ else x = x
3183 if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
3184 if_then_else_ x {P} = if_/_then_else_ x P
3186 data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
3187 _◁_ : (s : S) → (P s → W S P) → W S P
3189 rec : ∀ {a b} {S : Set a} {P : S → Set b}
3190 (C : W S P → Set) → -- some conclusion we hope holds
3191 ((s : S) → -- given a shape...
3192 (f : P s → W S P) → -- ...and a bunch of kids...
3193 ((p : P s) → C (f p)) → -- ...and C for each kid in the bunch...
3194 C (s ◁ f)) → -- ...does C hold for the node?
3195 (x : W S P) → -- If so, ...
3196 C x -- ...C always holds.
3197 rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
3199 module Examples-→ where
3206 -- These pragmas are needed so we can use number literals.
3207 {-# BUILTIN NATURAL ℕ #-}
3208 {-# BUILTIN ZERO zero #-}
3209 {-# BUILTIN SUC suc #-}
3211 data List (A : Set) : Set where
3213 _∷_ : A → List A → List A
3215 length : ∀ {A} → List A → ℕ
3217 length (_ ∷ l) = suc (length l)
3222 suc x > suc y = x > y
3224 head : ∀ {A} → (l : List A) → length l > 0 → A
3225 head [] p = absurd p
3228 module Examples-× where
3234 even (suc zero) = Empty
3235 even (suc (suc n)) = even n
3240 5-not-even : ¬ (even 5)
3243 there-is-an-even-number : ℕ × even
3244 there-is-an-even-number = 6 , 6-even
3246 _∨_ : (A B : Set) → Set
3247 A ∨ B = Bool × (λ b → if b then A else B)
3249 left : ∀ {A B} → A → A ∨ B
3252 right : ∀ {A B} → B → A ∨ B
3255 [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
3257 (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
3259 module Examples-W where
3264 Tr b = if b then Unit else Empty
3270 zero = false ◁ absurd
3273 suc n = true ◁ (λ _ → n)
3279 if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
3280 then (λ _ f → (suc (f tt))) else (λ _ _ → y))
3283 List : (A : Set) → Set
3284 List A = W (A ∨ Unit) (λ s → Tr (fst s))
3287 [] = (false , tt) ◁ absurd
3289 _∷_ : ∀ {A} → A → List A → List A
3290 x ∷ l = (true , x) ◁ (λ _ → l)
3292 _++_ : ∀ {A} → List A → List A → List A
3294 (λ _ → List _ → List _)
3298 module Equality where
3301 data _≡_ {a} {A : Set a} : A → A → Set a where
3304 ≡-elim : ∀ {a b} {A : Set a}
3305 (P : (x y : A) → x ≡ y → Set b) →
3306 ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
3307 ≡-elim P p (refl x) = p
3309 subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
3310 subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
3312 sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
3313 sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
3315 trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
3316 trans x y z p q = subst (λ z′ → x ≡ z′) q p
3318 cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y
3319 cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
3322 \subsubsection{\mykant}
3324 The following things are missing: $\mytyc{W}$-types, since our
3325 positivity check is overly strict, and equality, since we haven't
3326 implemented that yet.
3329 \verbatiminput{itt.ka}
3332 \subsection{\mykant\ examples}
3335 \verbatiminput{examples.ka}
3338 \subsection{\mykant's hierachy}
3340 This rendition of the Hurken's paradox does not type check with the
3341 hierachy enabled, type checks and loops without it. Adapted from an
3342 Agda version, available at
3343 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
3346 \verbatiminput{hurkens.ka}
3349 \bibliographystyle{authordate1}
3350 \bibliography{thesis}