2 %% THIS LATEX HURTS YOUR EYES. DO NOT READ.
5 \documentclass[11pt, fleqn, twoside]{article}
8 \usepackage[usenames,dvipsnames]{xcolor}
10 \usepackage[sc,slantedGreek]{mathpazo}
14 % \oddsidemargin .50in
15 % \evensidemargin -.25in
16 % % \oddsidemargin 0in
17 % % \evensidemargin 0in
26 \usepackage[hmargin=2cm,vmargin=2.5cm]{geometry}
27 \geometry{textwidth=390pt}
28 \geometry{bindingoffset=1.5cm}
38 \usepackage[pdftex, pdfborderstyle={/S/U/W 0}]{hyperref}
44 \usepackage[fleqn]{amsmath}
45 \usepackage{stmaryrd} %llbracket
48 \usepackage{bussproofs}
60 \usepackage{subcaption}
65 \RecustomVerbatimEnvironment
71 \usetikzlibrary{shapes,arrows,positioning}
72 \usetikzlibrary{intersections}
73 % \usepackage{tikz-cd}
74 % \usepackage{pgfplots}
79 \titleformat{\section}
80 {\normalfont\huge\scshape}
81 {\thesection\hskip 9pt\textpipe\hskip 9pt}
85 \newcommand{\sectionbreak}{\clearpage}
89 %% -----------------------------------------------------------------------------
91 \usepackage[english]{babel}
92 \usepackage[conor]{agda}
93 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
94 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
95 \renewcommand{\AgdaField}{\AgdaFunction}
96 % \definecolor{AgdaBound} {HTML}{000000}
97 \definecolor{AgdaHole} {HTML} {FFFF33}
99 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
100 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
101 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
102 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
103 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
104 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
106 \renewenvironment{code}%
107 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed\small}%
108 {\endpboxed\par\noindent%
109 \ignorespacesafterend\small}
112 %% -----------------------------------------------------------------------------
115 \newcommand{\mysmall}{}
116 \newcommand{\mysyn}{\AgdaKeyword}
117 \newcommand{\mytyc}[1]{\textup{\AgdaDatatype{#1}}}
118 \newcommand{\mydc}[1]{\textup{\AgdaInductiveConstructor{#1}}}
119 \newcommand{\myfld}[1]{\textup{\AgdaField{#1}}}
120 \newcommand{\myfun}[1]{\textup{\AgdaFunction{#1}}}
121 \newcommand{\myb}[1]{\AgdaBound{$#1$}}
122 \newcommand{\myfield}{\AgdaField}
123 \newcommand{\myind}{\AgdaIndent}
124 \newcommand{\mykant}{\textmd{\textsc{Bertus}}}
125 \newcommand{\mysynel}[1]{#1}
126 \newcommand{\myse}{\mysynel}
127 \newcommand{\mytmsyn}{\mysynel{term}}
128 \newcommand{\mysp}{\ }
129 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
130 \newcommand{\myappsp}{\hspace{0.07cm}}
131 \newcommand{\myapp}[2]{#1 \myappsp #2}
132 \newcommand{\mysynsep}{\ \ |\ \ }
133 \newcommand{\myITE}[3]{\myfun{If}\, #1\, \myfun{Then}\, #2\, \myfun{Else}\, #3}
134 \newcommand{\mycumul}{\preceq}
136 \newcommand{\mydesc}[3]{
142 \hfill \textup{\phantom{ygp}\textbf{#1}} $#2$
143 \framebox[\textwidth]{
158 \newcommand{\mytmt}{\mysynel{t}}
159 \newcommand{\mytmm}{\mysynel{m}}
160 \newcommand{\mytmn}{\mysynel{n}}
161 \newcommand{\myred}{\leadsto}
162 \newcommand{\mysub}[3]{#1[#3 / #2]}
163 \newcommand{\mytysyn}{\mysynel{type}}
164 \newcommand{\mybasetys}{K}
165 \newcommand{\mybasety}[1]{B_{#1}}
166 \newcommand{\mytya}{\myse{A}}
167 \newcommand{\mytyb}{\myse{B}}
168 \newcommand{\mytycc}{\myse{C}}
169 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
170 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
171 \newcommand{\myctx}{\Gamma}
172 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
173 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
174 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
175 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
176 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
177 \newcommand{\myunit}{\mytyc{Unit}}
178 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
179 \newcommand{\myfst}{\myfld{fst}}
180 \newcommand{\mysnd}{\myfld{snd}}
181 \newcommand{\myconst}{\myse{c}}
182 \newcommand{\myemptyctx}{\varepsilon}
183 \newcommand{\myhole}{\AgdaHole}
184 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
185 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
186 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
187 \newcommand{\myright}[1]{\mydc{right}_{#1}}
188 \newcommand{\myempty}{\mytyc{Empty}}
189 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
190 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
191 \newcommand{\myarg}{\_}
192 \newcommand{\myderivsp}{}
193 \newcommand{\myderivspp}{\vspace{0.3cm}}
194 \newcommand{\mytyp}{\mytyc{Type}}
195 \newcommand{\myneg}{\myfun{$\neg$}}
196 \newcommand{\myar}{\,}
197 \newcommand{\mybool}{\mytyc{Bool}}
198 \newcommand{\mytrue}{\mydc{true}}
199 \newcommand{\myfalse}{\mydc{false}}
200 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
201 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
202 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
203 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
204 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
205 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
206 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
207 \newcommand{\mylist}{\mytyc{List}}
208 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
209 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
210 \newcommand{\myfoldr}{\myfun{foldr}}
211 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
212 \newcommand{\mynodee}{\mathbin{\mydc{$\lhd$}}}
213 \newcommand{\mynode}[2]{\mynodee_{#1.#2}}
214 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
215 \newcommand{\mylub}{\sqcup}
216 \newcommand{\mydefeq}{\cong}
217 \newcommand{\myrefl}{\mydc{refl}}
218 \newcommand{\mypeq}{\mytyc{=}}
219 \newcommand{\myjeqq}{\myfun{$=$-elim}}
220 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
221 \newcommand{\mysubst}{\myfun{subst}}
222 \newcommand{\myprsyn}{\myse{prop}}
223 \newcommand{\myprdec}[1]{\mathopen{\mytyc{$\llbracket$}} #1 \mathclose{\mytyc{$\rrbracket$}}}
224 \newcommand{\myand}{\mathrel{\mytyc{$\wedge$}}}
225 \newcommand{\mybigand}{\mathrel{\mytyc{$\bigwedge$}}}
226 \newcommand{\myprfora}[3]{\forall #1 {:} #2.\, #3}
227 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
228 \newcommand{\mybot}{\mytyc{$\bot$}}
229 \newcommand{\mytop}{\mytyc{$\top$}}
230 \newcommand{\mycoe}{\myfun{coe}}
231 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
232 \newcommand{\mycoh}{\myfun{coh}}
233 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
234 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
235 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
236 \newcommand{\myprop}{\mytyc{Prop}}
237 \newcommand{\mytmup}{\mytmsyn\uparrow}
238 \newcommand{\mydefs}{\Delta}
239 \newcommand{\mynf}{\Downarrow}
240 \newcommand{\myinff}[3]{#1 \vdash #2 \Uparrow #3}
241 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
242 \newcommand{\mychkk}[3]{#1 \vdash #2 \Downarrow #3}
243 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
244 \newcommand{\myann}[2]{#1 : #2}
245 \newcommand{\mydeclsyn}{\myse{decl}}
246 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
247 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
248 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
249 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ \{ #4 \}}
250 \newcommand{\myelabt}{\vdash}
251 \newcommand{\myelabf}{\rhd}
252 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
253 \newcommand{\mytele}{\Delta}
254 \newcommand{\mytelee}{\delta}
255 \newcommand{\mydcctx}{\Gamma}
256 \newcommand{\mynamesyn}{\myse{name}}
257 \newcommand{\myvec}{\overrightarrow}
258 \newcommand{\mymeta}{\textsc}
259 \newcommand{\myhyps}{\mymeta{hyps}}
260 \newcommand{\mycc}{;}
261 \newcommand{\myemptytele}{\varepsilon}
262 \newcommand{\mymetagoes}{\Longrightarrow}
263 % \newcommand{\mytesctx}{\
264 \newcommand{\mytelesyn}{\myse{telescope}}
265 \newcommand{\myrecs}{\mymeta{recs}}
266 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
267 \newcommand{\mylet}{\mysyn{let}}
268 \newcommand{\myhead}{\mymeta{head}}
269 \newcommand{\mytake}{\mymeta{take}}
270 \newcommand{\myix}{\mymeta{ix}}
271 \newcommand{\myapply}{\mymeta{apply}}
272 \newcommand{\mydataty}{\mymeta{datatype}}
273 \newcommand{\myisreco}{\mymeta{record}}
274 \newcommand{\mydcsep}{\ |\ }
275 \newcommand{\mytree}{\mytyc{Tree}}
276 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
277 \newcommand{\mysigma}{\mytyc{$\Sigma$}}
278 \newcommand{\mynegder}{\vspace{-0.3cm}}
279 \newcommand{\myquot}{\Uparrow}
280 \newcommand{\mynquot}{\, \Downarrow}
281 \newcommand{\mycanquot}{\ensuremath{\textsc{quote}{\Downarrow}}}
282 \newcommand{\myneuquot}{\ensuremath{\textsc{quote}{\Uparrow}}}
283 \newcommand{\mymetaguard}{\ |\ }
284 \newcommand{\mybox}{\Box}
285 \newcommand{\mytermi}[1]{\text{\texttt{#1}}}
286 \newcommand{\mysee}[1]{\langle\myse{#1}\rangle}
288 \renewcommand{\[}{\begin{equation*}}
289 \renewcommand{\]}{\end{equation*}}
290 \newcommand{\mymacol}[2]{\text{\textcolor{#1}{$#2$}}}
292 \newtheorem*{mydef}{Definition}
293 \newtheoremstyle{named}{}{}{\itshape}{}{\bfseries}{}{.5em}{\textsc{#1}}
296 \pgfdeclarelayer{background}
297 \pgfdeclarelayer{foreground}
298 \pgfsetlayers{background,main,foreground}
300 \definecolor{webgreen}{rgb}{0,.5,0}
301 \definecolor{webbrown}{rgb}{.6,0,0}
302 \definecolor{webyellow}{rgb}{0.98,0.92,0.73}
305 colorlinks=true, linktocpage=true, pdfstartpage=3, pdfstartview=FitV,
306 breaklinks=true, pdfpagemode=UseNone, pageanchor=true, pdfpagemode=UseOutlines,
307 plainpages=false, bookmarksnumbered, bookmarksopen=true, bookmarksopenlevel=1,
308 hypertexnames=true, pdfhighlight=/O, urlcolor=webbrown, linkcolor=black, citecolor=webgreen}
311 %% -----------------------------------------------------------------------------
313 \title{\mykant: Implementing Observational Equality}
314 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
325 \pagenumbering{gobble}
330 % Upper part of the page. The '~' is needed because \\
331 % only works if a paragraph has started.
332 \includegraphics[width=0.4\textwidth]{brouwer-cropped.png}~\\[1cm]
334 \textsc{\Large Final year project}\\[0.5cm]
337 { \huge \mykant: Implementing Observational Equality}\\[1.5cm]
339 {\Large Francesco \textsc{Mazzoli} \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}\\[0.8cm]
341 \begin{minipage}{0.4\textwidth}
342 \begin{flushleft} \large
344 Dr. Steffen \textsc{van Bakel}
347 \begin{minipage}{0.4\textwidth}
348 \begin{flushright} \large
349 \emph{Second marker:} \\
350 Dr. Philippa \textsc{Gardner}
366 The marriage between programming and logic has been a fertile one. In
367 particular, since the definition of the simply typed
368 $\lambda$-calculus, a number of type systems have been devised with
369 increasing expressive power.
371 Among this systems, Intuitionistic Type Theory (ITT) has been a
372 popular framework for theorem provers and programming languages.
373 However, reasoning about equality has always been a tricky business in
374 ITT and related theories. In this thesis we shall explain why this is
375 the case, and present Observational Type Theory (OTT), a solution to
376 some of the problems with equality.
378 To bring OTT closer to the current practice of interactive theorem
379 provers, we describe \mykant, a system featuring OTT in a setting more
380 close to the one found in widely used provers such as Agda and Coq.
381 Most notably, we feature used defined inductive and record types and a
382 cumulative, implicit type hierarchy. Having implemented part of
383 $\mykant$ as a Haskell program, we describe some of the implementation
392 \renewcommand{\abstractname}{Acknowledgements}
394 I would like to thank Steffen van Bakel, my supervisor, who was brave
395 enough to believe in my project and who provided support and
398 I would also like to thank the Haskell and Agda community on
399 \texttt{IRC}, which guided me through the strange world of types; and
400 in particular Andrea Vezzosi and James Deikun, with whom I entertained
401 countless insightful discussions over the past year. Andrea suggested
402 Observational Type Theory as a topic of study: this thesis would not
403 exist without him. Before them, Tony Field introduced me to Haskell,
404 unknowingly filling most of my free time from that time on.
406 Finally, most of the work stems from the research of Conor McBride,
407 who answered many of my doubts through these months. I also owe him
417 \section{Introduction}
419 \pagenumbering{arabic}
421 Functional programming is in good shape. In particular the `well-typed'
422 line of work originating from Milner's ML has been extremely fruitful,
423 in various directions. Nowadays functional, well-typed programming
424 languages like Haskell or OCaml are slowly being absorbed by the
425 mainstream. An important related development---and in fact the original
426 motivator for ML's existence---is the advancement of the practice of
427 \emph{interactive theorem provers}.
430 An interactive theorem prover, or proof assistant, is a tool that lets
431 the user develop formal proofs with the confidence of the machine
432 checking them for correctness. While the effort towards a full
433 formalisation of mathematics has been ongoing for more than a century,
434 theorem provers have been the first class of software whose
435 implementation depends directly on these theories.
437 In a fortunate turn of events, it was discovered that well-typed
438 functional programming and proving theorems in an \emph{intuitionistic}
439 logic are the same activity. Under this discipline, the types in our
440 programming language can be interpreted as proposition in our logic; and
441 the programs implementing the specification given by the types as their
442 proofs. This fact stimulated an active transfer of techniques and
443 knowledge between logic and programming language theory, in both
446 Mathematics could provide programming with a wealth of abstractions and
447 constructs developed over centuries. Moreover, identifying our types
448 with a logic lets us focus on foundational questions regarding
449 programming with a much more solid approach, given the years of rigorous
450 study of logic. Programmers, on the other hand, had already developed a
451 number of approaches to effectively collaborate with computers, through
452 the study of programming languages.
454 In this space, we shall follow the discipline of Intuitionistic Type
455 Theory, or Martin-L\"{o}f Type Theory, after its inventor. First
456 formulated in the 70s and then adjusted through a series of revisions,
457 it has endured as the core of many practical systems widely in use
458 today, and it is the most prominent instance of the proposition-as-types
459 and proofs-as-programs paradigm. One of the most debated subjects in
460 this field has been regarding what notion of equality should be
463 The tension when studying equality in type theory springs from the fact
464 that there is a divide between what the user can prove equal
465 \emph{inside} the theory---what is \emph{propositionally} equal---and
466 what the theorem prover identifies as equal in its meta-theory---what is
467 \emph{definitionally} equal. If we want our system to be well behaved
468 (mostly if we want to keep type checking decidable) we must keep the two
469 notions separate, with definitional equality inducing propositional
470 equality, but not the reverse. However in this scenario propositional
471 equality is weaker than we would like: we can only prove terms equal
472 based on their syntactical structure, and not based on their behaviour.
474 This thesis is concerned with exploring a new approach in this area,
475 \emph{observational} equality. Promising to provide a more adequate
476 propositional equality while retaining well-behavedness, it still is a
477 relatively unexplored notion. We set ourselves to change that by
478 studying it in a setting more akin to the one found in currently
479 available theorem provers.
481 \subsection{Structure}
483 Section \ref{sec:types} will give a brief overview of the
484 $\lambda$-calculus, both typed and untyped. This will give us the
485 chance to introduce most of the concepts mentioned above rigorously, and
486 gain some intuition about them. An excellent introduction to types in
487 general can be found in \cite{Pierce2002}, although not from the
488 perspective of theorem proving.
490 Section \ref{sec:itt} will describe a set of basic construct that form a
491 `baseline' Intuitionistic Type Theory. The goal is to familiarise with
492 the main concept of ITT before attacking the problem of equality. Given
493 the wealth of material covered the exposition is quite dense. Good
494 introductions can be found in \cite{Thompson1991}, \cite{Nordstrom1990},
495 and \cite{Martin-Lof1984} himself.
497 Section \ref{sec:equality} will introduce propositional equality. The
498 properties of propositional equality will be discussed along with its
499 limitations. After reviewing some extensions to propositional equality,
500 we will explain why identifying definitional equality with propositional
501 equality causes problems.
503 Section \ref{sec:ott} will introduce observational equality, following
504 closely the original exposition by \cite{Altenkirch2007}. The
505 presentation is free-standing but glosses over the meta-theoretic
506 properties of OTT, focusing on the mechanisms that make it work.
508 Section \ref{sec:kant-theory} is the central part of the thesis and will
509 describe \mykant, a system we have developed incorporating OTT along
510 constructs usually present in modern theorem provers. Along the way, we
511 discuss these additional features and their trade-offs. Section
512 \ref{sec:kant-practice} will describe an implementation implementing
513 part of \mykant. A high level design of the software is given, along
514 with a few specific implementation issues.
516 Finally, Section \ref{sec:evaluation} will asses the decisions made in
517 designing and implementing \mykant and the results achieved; and Section
518 \ref{sec:future-work} will give a roadmap to bring \mykant\ on par and
519 beyond the competition.
521 \subsection{Contributions}
522 \label{sec:contributions}
524 The contribution of this thesis is threefold:
527 \item Provide a description of observational equality `in context', to
528 make the subject more accessible. Considering the possibilities that
529 OTT brings to the table, we think that introducing it to a wider
530 audience can only be beneficial.
532 \item Fill in the gaps needed to make OTT work with user-defined
533 inductive types and a type hierarchy. We show how one notion of
534 equality is enough, instead of separate notions of value- and
535 type-equality as presented in the original paper. We are able to keep
536 the type equalities `small' while preserving subject reduction by
537 exploiting the fact that we work within a cumulative theory.
538 Incidentally, we also describe a generalised version of bidirectional
539 type checking for user defined types.
541 \item Provide an implementation to probe the possibilities of OTT in a
542 more realistic setting. We have implemented an ITT with user defined
543 types but due to the limited time constraints we were not able to
544 complete the implementation of observational equality. Nonetheless,
545 we describe some interesting implementation issues faced by the type
549 The system developed as part of this thesis, \mykant, incorporates OTT
550 with features that are familiar to users of existing theorem provers
551 adopting the proofs-as-programs mantra. The defining features of
555 \item[Full dependent types] In ITT, types are very `first class' notion
556 and can be the result of computation---they can \emph{depend} on
557 values, thus the name \emph{dependent types}. \mykant\ espouses this
558 notion to its full consequences.
560 \item[User defined data types and records] Instead of forcing the user
561 to choose from a restricted toolbox, we let her define types for
562 greater flexibility. We have two kinds of user defined types:
563 inductive data types, formed by various data constructors whose type
564 signatures can contain recursive occurrences of the type being
565 defined; and records, where we have just one data constructor and
566 projections to extract each each field in said constructor.
568 \item[Consistency] Our system is meant to be consistent with respects to
569 the logic it embodies. For this reason, we restrict recursion to
570 \emph{structural} recursion on the defined inductive types, through
571 the use of operators (destructors) computing on each type. Following
572 the types-as-propositions interpretation, each destructor expresses an
573 induction principle on the data type it operates on. To achieve the
574 consistency of these operations we make sure that our recursive data
575 types are \emph{strictly positive}.
577 \item[Bidirectional type checking] We take advantage of a
578 \emph{bidirectional} type inference system in the style of
579 \cite{Pierce2000}. This cuts down the type annotations by a
580 considerable amount in an elegant way and at a very low cost.
581 Bidirectional type checking is usually employed in core calculi, but
582 in \mykant\ we extend the concept to user defined data types.
584 \item[Type hierarchy] In set theory we have to take treat powerset-like
585 objects with care, if we want to avoid paradoxes. However, the
586 working mathematician is rarely concerned by this, and the consistency
587 in this regard is implicitly assumed. In the tradition of
588 \cite{Russell1927}, in \mykant\ we employ a \emph{type hierarchy} to
589 make sure that these size issues are taken care of; and we employ
590 system so that the user will be free from thinking about the
591 hierarchy, just like the mathematician is.
593 \item[Observational equality] The motivator of this thesis, \mykant\
594 incorporates a notion of observational equality, modifying the
595 original presentation by \cite{Altenkirch2007} to fit our more
596 expressive system. As mentioned, we reconcile OTT with user defined
597 types and a type hierarchy.
599 \item[Type holes] When building up programs interactively, it is useful
600 to leave parts unfinished while exploring the current context. This
601 is what type holes are for.
604 \subsection{Notation and syntax}
606 Appendix \ref{app:notation} describes the notation and syntax used in
609 \section{Simple and not-so-simple types}
612 \epigraph{\emph{Well typed programs can't go wrong.}}{Robin Milner}
614 \subsection{The untyped $\lambda$-calculus}
617 Along with Turing's machines, the earliest attempts to formalise
618 computation lead to the definition of the $\lambda$-calculus
619 \citep{Church1936}. This early programming language encodes computation
620 with a minimal syntax and no `data' in the traditional sense, but just
621 functions. Here we give a brief overview of the language, which will
622 give the chance to introduce concepts central to the analysis of all the
623 following calculi. The exposition follows the one found in Chapter 5 of
626 \begin{mydef}[$\lambda$-terms]
627 Syntax of the $\lambda$-calculus: variables, abstractions, and
633 \begin{array}{r@{\ }c@{\ }l}
634 \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
635 x & \in & \text{Some enumerable set of symbols}
640 Parenthesis will be omitted in the usual way, with application being
643 Abstractions roughly corresponds to functions, and their semantics is more
644 formally explained by the $\beta$-reduction rule.
646 \begin{mydef}[$\beta$-reduction]
647 $\beta$-reduction and substitution for the $\lambda$-calculus.
650 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
653 \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{ \textbf{where}} \\
655 \begin{array}{l@{\ }c@{\ }l}
656 \mysub{\myb{y}}{\myb{x}}{\mytmn} \mymetaguard \myb{x} = \myb{y} & \mymetagoes & \mytmn \\
657 \mysub{\myb{y}}{\myb{x}}{\mytmn} & \mymetagoes & \myb{y} \\
658 \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
659 \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & \myabs{\myb{x}}{\mytmm} \\
660 \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & \mymetagoes & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}} \\
661 \multicolumn{3}{l}{\myind{2} \text{\textbf{with} $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
667 The care required during substituting variables for terms is to avoid
668 name capturing. We will use substitution in the future for other
669 name-binding constructs assuming similar precautions.
671 These few elements have a remarkable expressiveness, and are in fact
672 Turing complete. As a corollary, we must be able to devise a term that
673 reduces forever (`loops' in imperative terms):
675 (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, \textbf{where} $\omega = \myabs{x}{\myapp{x}{x}}$}
678 A \emph{redex} is a term that can be reduced.
680 In the untyped $\lambda$-calculus this will be the case for an
681 application in which the first term is an abstraction, but in general we
682 call a term reducible if it appears to the left of a reduction rule.
683 \begin{mydef}[normal form]
684 A term that contains no redexes is said to be in \emph{normal form}.
686 \begin{mydef}[normalising terms and systems]
687 Terms that reduce in a finite number of reduction steps to a normal
688 form are \emph{normalising}. A system in which all terms are
689 normalising is said to have the \emph{normalisation property}, or
690 to be \emph{normalising}.
692 Given the reduction behaviour of $(\myapp{\omega}{\omega})$, it is clear
693 that the untyped $\lambda$-calculus does not have the normalisation
696 We have not presented reduction in an algorithmic way, but
697 \emph{evaluation strategies} can be employed to reduce term
698 systematically. Common evaluation strategies include \emph{call by
699 value} (or \emph{strict}), where arguments of abstractions are reduced
700 before being applied to the abstraction; and conversely \emph{call by
701 name} (or \emph{lazy}), where we reduce only when we need to do so to
702 proceed---in other words when we have an application where the function
703 is still not a $\lambda$. In both these strategies we never
704 reduce under an abstraction. For this reason a weaker form of
705 normalisation is used, where all abstractions are said to be in
706 \emph{weak head normal form} even if their body is not.
708 \subsection{The simply typed $\lambda$-calculus}
710 A convenient way to `discipline' and reason about $\lambda$-terms is to
711 assign \emph{types} to them, and then check that the terms that we are
712 forming make sense given our typing rules \citep{Curry1934}. The first
713 most basic instance of this idea takes the name of \emph{simply typed
714 $\lambda$-calculus} (STLC).
715 \begin{mydef}[Simply typed $\lambda$-calculus]
716 The syntax and typing rules for the STLC are given in Figure \ref{fig:stlc}.
719 Our types contain a set of \emph{type variables} $\Phi$, which might
720 correspond to some `primitive' types; and $\myarr$, the type former for
721 `arrow' types, the types of functions. The language is explicitly
722 typed: when we bring a variable into scope with an abstraction, we
723 declare its type. Reduction is unchanged from the untyped
729 \begin{array}{r@{\ }c@{\ }l}
730 \mytmsyn & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
731 (\myapp{\mytmsyn}{\mytmsyn}) \\
732 \mytysyn & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn \mysynsep \\
733 \myb{x} & \in & \text{Some enumerable set of symbols} \\
734 \myse{\phi} & \in & \Phi
739 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
741 \AxiomC{$\myctx(x) = A$}
742 \UnaryInfC{$\myjud{\myb{x}}{A}$}
745 \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
746 \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
749 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
750 \AxiomC{$\myjud{\mytmn}{\mytya}$}
751 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
755 \caption{Syntax and typing rules for the STLC. Reduction is unchanged from
756 the untyped $\lambda$-calculus.}
760 In the typing rules, a context $\myctx$ is used to store the types of
761 bound variables: $\myemptyctx$ is the empty context, and $\myctx;
762 \myb{x} : \mytya$ adds a variable to the context. $\myctx(x)$ extracts
763 the type of the rightmost occurrence of $x$.
765 This typing system takes the name of `simply typed lambda calculus' (STLC), and
766 enjoys a number of properties. Two of them are expected in most type systems
768 \begin{mydef}[Progress]
769 A well-typed term is not stuck---it is either a variable, or it does
770 not appear on the left of the $\myred$ relation , or it can take a
771 step according to the evaluation rules.
773 \begin{mydef}[Subject reduction]
774 If a well-typed term takes a step of evaluation, then the
775 resulting term is also well-typed, and preserves the previous type.
778 However, STLC buys us much more: every well-typed term is normalising
779 \citep{Tait1967}. It is easy to see that we cannot fill the blanks if we want to
780 give types to the non-normalising term shown before:
782 \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
784 This makes the STLC Turing incomplete. We can recover the ability to loop by
785 adding a combinator that recurses:
786 \begin{mydef}[Fixed-point combinator]\end{mydef}
789 \begin{minipage}{0.5\textwidth}
791 $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
795 \begin{minipage}{0.5\textwidth}
796 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
797 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
798 \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
803 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
804 $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
807 This will deprive us of normalisation, which is a particularly bad thing if we
808 want to use the STLC as described in the next section.
810 Another important property of the STLC is the Church-Rosser property:
811 \begin{mydef}[Church-Rosser property]
812 A system is said to have the \emph{Church-Rosser} property, or to be
813 \emph{confluent}, if given any two reductions $\mytmm$ and $\mytmn$ of
814 a given term $\mytmt$, there is exist a term to which both $\mytmm$
815 and $\mytmn$ can be reduced.
817 Given that the STLC has the normalisation property and the Church-Rosser
818 property, each term has a \emph{unique} normal form.
820 \subsection{The Curry-Howard correspondence}
822 As hinted in the introduction, it turns out that the STLC can be seen a
823 natural deduction system for intuitionistic propositional logic. Terms
824 correspond to proofs, and their types correspond to the propositions
825 they prove. This remarkable fact is known as the Curry-Howard
826 correspondence, or isomorphism.
828 The arrow ($\myarr$) type corresponds to implication. If we wish to prove that
829 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
830 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
833 \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
835 Which is known to functional programmers as function composition. Going
836 beyond arrow types, we can extend our bare lambda calculus with useful
837 types to represent other logical constructs.
838 \begin{mydef}[The extended STLC]
839 Figure \ref{fig:natded} shows syntax, reduction, and typing rules for
840 the \emph{extended simply typed $\lambda$-calculus}.
846 \begin{array}{r@{\ }c@{\ }l}
847 \mytmsyn & ::= & \cdots \\
848 & | & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
849 & | & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
850 \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
851 \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
852 & | & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
853 \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
854 \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
859 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
862 \begin{array}{l@{ }l@{\ }c@{\ }l}
863 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
864 \myapp{\mytmm}{\mytmt} \\
865 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
866 \myapp{\mytmn}{\mytmt}
871 \begin{array}{l@{ }l@{\ }c@{\ }l}
872 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
873 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
879 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
881 \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
882 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
885 \AxiomC{$\myjud{\mytmt}{\myempty}$}
886 \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
893 \AxiomC{$\myjud{\mytmt}{\mytya}$}
894 \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
897 \AxiomC{$\myjud{\mytmt}{\mytyb}$}
898 \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
906 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
907 \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
908 \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
909 \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
916 \AxiomC{$\myjud{\mytmm}{\mytya}$}
917 \AxiomC{$\myjud{\mytmn}{\mytyb}$}
918 \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
921 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
922 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
925 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
926 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
930 \caption{Rules for the extended STLC. Only the new features are shown, all the
931 rules and syntax for the STLC apply here too.}
935 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
936 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
937 products---$\myprod$ here, tuples in Haskell) correspond to
938 conjunctions. This is apparent looking at the ways to construct and
939 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
940 and $\myright{ }$ correspond to $\vee$ introduction, and
941 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
942 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
943 and $\mysnd$ to $\wedge$ elimination.
945 The trivial type $\myunit$ corresponds to the logical $\top$ (true), and
946 dually $\myempty$ corresponds to the logical $\bot$ (false). $\myunit$
947 has one introduction rule ($\mytt$), and thus one inhabitant; and no
948 eliminators---we cannot gain any information from a witness of the
949 single member of $\myunit$. $\myempty$ has no introduction rules, and
950 thus no inhabitants; and one eliminator ($\myabsurd{ }$), corresponding
951 to the logical \emph{ex falso quodlibet}.
953 With these rules, our STLC now looks remarkably similar in power and use to the
954 natural deduction we already know.
955 \begin{mydef}[Negation]
956 $\myneg \mytya$ can be expressed as $\mytya \myarr \myempty$.
958 However, there is an important omission: there is no term of
959 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
960 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
961 a type equivalent to those.
963 This has a considerable effect on our logic and it is no coincidence, since there
964 is no obvious computational behaviour for laws like the excluded middle.
965 Logics of this kind are called \emph{intuitionistic}, or \emph{constructive},
966 and all the systems analysed will have this characteristic since they build on
967 the foundation of the STLC.\footnote{There is research to give computational
968 behaviour to classical logic, but I will not touch those subjects.}
970 As in logic, if we want to keep our system consistent, we must make sure that no
971 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
972 The variant of STLC presented here is indeed
973 consistent, a result that follows from the fact that it is
975 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
976 desire for consistency. The following term works for every type $\mytya$,
978 \[(\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya\]
980 \subsection{Inductive data}
983 To make the STLC more useful as a programming language or reasoning tool it is
984 common to include (or let the user define) inductive data types. These comprise
985 of a type former, various constructors, and an eliminator (or destructor) that
986 serves as primitive recursor.
988 \begin{mydef}[Finite lists for the STLC]
989 We add a $\mylist$ type constructor, along with an `empty
990 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor. The eliminator for
991 lists will be the usual folding operation ($\myfoldr$). Full rules in Figure
998 \begin{array}{r@{\ }c@{\ }l}
999 \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
1001 \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
1002 \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
1006 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1008 \begin{array}{l@{\ }c@{\ }l}
1009 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
1011 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
1012 \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
1016 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
1018 \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
1019 \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
1022 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1023 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
1024 \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
1029 \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
1030 \AxiomC{$\myjud{\mytmm}{\mytyb}$}
1031 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
1032 \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
1035 \caption{Rules for lists in the STLC.}
1039 In Section \ref{sec:well-order} we will see how to give a general account of
1042 \section{Intuitionistic Type Theory}
1045 \epigraph{\emph{Martin-L{\"o}f's type theory is a well established and
1046 convenient arena in which computational Christians are regularly
1047 fed to logical lions.}}{Conor McBride}
1049 \subsection{Extending the STLC}
1051 \cite{Barendregt1991} succinctly expressed geometrically how we can add
1052 expressively to the STLC:
1054 \xymatrix@!0@=1.5cm{
1055 & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
1056 & & \lambda C \ar@{-}[dd]
1058 \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
1059 & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
1061 & \lambda\underline\omega \ar@{-}'[r][rr]
1062 & & \lambda P\underline\omega
1064 \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
1065 & & \lambda P \ar@{-}[ur]
1068 Here $\lambda{\to}$, in the bottom left, is the STLC. From there can move along
1071 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
1072 types in our type signatures. For example, we can define a polymorphic
1073 identity function, where $\mytyp$ denotes the `type of types':
1075 (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} {:} \mytyp) \myarr \myb{A} \myarr \myb{A}
1077 The first and most famous instance of this idea has been System F.
1078 This form of polymorphism and has been wildly successful, also thanks
1079 to a well known inference algorithm for a restricted version of System
1080 F known as Hindley-Milner \citep{milner1978theory}. Languages like
1081 Haskell and SML are based on this discipline. In Haskell the above
1087 Where \texttt{a} implicitly quantifies over a type, and will be
1088 instantiated automatically thanks to the inference.
1089 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
1090 type operators. For example we could define a function that given types $R$
1091 and $\mytya$ forms the type that represents a value of type $\mytya$ in
1092 continuation passing style:
1093 \[\displaystyle(\myabss{\myb{R} \myarr \myb{A}}{\mytyp}{(\myb{A}
1094 \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp
1096 In Haskell we can define type operator of sorts, although we must
1097 pair them with data constructors, to keep inference manageable:
1099 newtype Cont r a = Cont ((a -> r) -> r)
1101 Where the `type' (kind in Haskell parlance) of \texttt{Cont} will be
1102 \texttt{* -> * -> *}, with \texttt{*} signifying the type of types in
1104 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
1105 types', give great expressive power. For example, we can have values of whose
1106 type depend on a boolean:
1107 \[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
1108 \myarr \mytyp\] We cannot give an Haskell example that expresses this
1109 concept since Haskell does not support dependent types---it would be a
1110 very different language if it did.
1113 All the systems preserve the properties that make the STLC well behaved. The
1114 system we are going to focus on, Intuitionistic Type Theory, has all of the
1115 above additions, and thus would sit where $\lambda{C}$ sits in the
1116 `$\lambda$-cube'. It will serve as the logical `core' of all the other
1117 extensions that we will present and ultimately our implementation of a similar
1120 \subsection{A Bit of History}
1122 Logic frameworks and programming languages based on type theory have a
1123 long history. Per Martin-L\"{o}f described the first version of his
1124 theory in 1971, but then revised it since the original version was
1125 inconsistent due to its impredicativity.\footnote{In the early version
1126 there was only one universe $\mytyp$ and $\mytyp : \mytyp$; see
1127 Section \ref{sec:term-types} for an explanation on why this causes
1128 problems.} For this reason he later gave a revised and consistent
1129 definition \citep{Martin-Lof1984}.
1131 A related development is the polymorphic $\lambda$-calculus, and specifically
1132 the previously mentioned System F, which was developed independently by Girard
1133 and Reynolds. An overview can be found in \citep{Reynolds1994}. The surprising
1134 fact is that while System F is impredicative it is still consistent and strongly
1135 normalising. \cite{Coquand1986} further extended this line of work with the
1136 Calculus of Constructions (CoC).
1138 Most widely used interactive theorem provers are based on ITT. Popular
1139 ones include Agda \citep{Norell2007}, Coq \citep{Coq}, Epigram
1140 \citep{McBride2004, EpigramTut}, Isabelle \citep{Paulson1990}, and many
1143 \subsection{A simple type theory}
1146 The calculus I present follows the exposition in \cite{Thompson1991},
1147 and is quite close to the original formulation of \cite{Martin-Lof1984}.
1148 Agda and \mykant\ renditions of the presented theory and all the
1149 examples (even the ones presented only as type signatures) are
1150 reproduced in Appendix \ref{app:itt-code}.
1151 \begin{mydef}[Intuitionistic Type Theory (ITT)]
1152 The syntax and reduction rules are shown in Figure \ref{fig:core-tt-syn}.
1153 The typing rules are presented piece by piece in the following sections.
1159 \begin{array}{r@{\ }c@{\ }l}
1160 \mytmsyn & ::= & \myb{x} \mysynsep
1161 \mytyp_{level} \mysynsep
1162 \myunit \mysynsep \mytt \mysynsep
1163 \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
1164 & | & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
1165 \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1166 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1167 \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1168 (\myapp{\mytmsyn}{\mytmsyn}) \\
1169 & | & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1170 \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
1171 & | & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
1172 & | & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1173 \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
1174 & | & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
1175 level & \in & \mathbb{N}
1180 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1181 \begin{tabular}{ccc}
1183 \begin{array}{l@{ }l@{\ }c@{\ }l}
1184 \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
1185 \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
1190 \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
1194 \begin{array}{l@{ }l@{\ }c@{\ }l}
1195 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
1196 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
1204 \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
1205 \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
1206 \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
1210 \caption{Syntax and reduction rules for our type theory.}
1211 \label{fig:core-tt-syn}
1214 \subsubsection{Types are terms, some terms are types}
1215 \label{sec:term-types}
1217 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1219 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1220 \AxiomC{$\mytya \mydefeq \mytyb$}
1221 \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
1224 \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
1225 \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
1230 The first thing to notice is that a barrier between values and types that we had
1231 in the STLC is gone: values can appear in types, and the two are treated
1232 uniformly in the syntax.
1234 While the usefulness of doing this will become clear soon, a consequence is
1235 that since types can be the result of computation, deciding type equality is
1236 not immediate as in the STLC.
1237 \begin{mydef}[Definitional equality]
1238 We define \emph{definitional
1239 equality}, $\mydefeq$, as the congruence relation extending
1240 $\myred$. Moreover, when comparing types syntactically we do it up to
1241 renaming of bound names ($\alpha$-renaming)
1243 For example under this discipline we will find that
1246 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}} \\
1247 \myapp{(\myabss{\myb{f}}{\mytya \myarr \mytya}{\myb{f}})}{(\myabss{\myb{y}}{\mytya}{\myb{y}})} \mydefeq \myabss{\myb{quux}}{\mytya}{\myb{quux}}
1250 Types that are definitionally equal can be used interchangeably. Here
1251 the `conversion' rule is not syntax directed, but it is possible to
1252 employ $\myred$ to decide term equality in a systematic way, comparing
1253 terms by reducing to their normal forms and then comparing them
1254 syntactically; so that a separate conversion rule is not needed.
1255 Another thing to notice is that, considering the need to reduce terms to
1256 decide equality, for type checking to be decidable a dependently typed
1257 must be terminating and confluent; since every type needs to have a
1258 unique normal form for definitional equality to be decidable.
1260 Moreover, we specify a \emph{type hierarchy} to talk about `large'
1261 types: $\mytyp_0$ will be the type of types inhabited by data:
1262 $\mybool$, $\mynat$, $\mylist$, etc. $\mytyp_1$ will be the type of
1263 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
1264 \mytyp_0 : \mytyp_1 : \cdots$. Each type `level' is often called a
1265 universe in the literature. While it is possible to simplify things by
1266 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
1267 inconsistent for much the same reason that impredicative na\"{\i}ve set
1268 theory is \citep{Hurkens1995}. However various techniques can be
1269 employed to lift the burden of explicitly handling universes, as we will
1270 see in Section \ref{sec:term-hierarchy}.
1272 \subsubsection{Contexts}
1274 \begin{minipage}{0.5\textwidth}
1275 \mydesc{context validity:}{\myvalid{\myctx}}{
1277 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1278 \UnaryInfC{$\myvalid{\myemptyctx}$}
1281 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1282 \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
1287 \begin{minipage}{0.5\textwidth}
1288 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1289 \AxiomC{$\myctx(x) = \mytya$}
1290 \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
1296 We need to refine the notion context to make sure that every variable appearing
1297 is typed correctly, or that in other words each type appearing in the context is
1298 indeed a type and not a value. In every other rule, if no premises are present,
1299 we assume the context in the conclusion to be valid.
1301 Then we can re-introduce the old rule to get the type of a variable for a
1304 \subsubsection{$\myunit$, $\myempty$}
1306 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1307 \begin{tabular}{ccc}
1308 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1309 \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
1311 \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
1314 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1315 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
1317 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
1320 \AxiomC{$\myjud{\mytmt}{\myempty}$}
1321 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1322 \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
1324 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
1329 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
1330 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
1331 sure that we are invoking $\myabsurd{}$ over a type.
1333 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
1335 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1336 \begin{tabular}{ccc}
1338 \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
1342 \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
1346 \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
1351 \AxiomC{$\myjud{\mytmt}{\mybool}$}
1352 \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
1354 \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
1355 \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
1359 With booleans we get the first taste of the `dependent' in `dependent
1360 types'. While the two introduction rules for $\mytrue$ and $\myfalse$
1361 are not surprising, the typing rules for $\myfun{if}$ are. In most
1362 strongly typed languages we expect the branches of an $\myfun{if}$
1363 statements to be of the same type, to preserve subject reduction, since
1364 execution could take both paths. This is a pity, since the type system
1365 does not reflect the fact that in each branch we gain knowledge on the
1366 term we are branching on. Which means that programs along the lines of
1368 if null xs then head xs else 0
1370 are a necessary, well-typed, danger.
1372 However, in a more expressive system, we can do better: the branches' type can
1373 depend on the value of the scrutinised boolean. This is what the typing rule
1374 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1375 representing the scrutinised boolean type, and the branches are type checked with
1376 the updated knowledge of the value of $\myb{x}$.
1378 \subsubsection{$\myarr$, or dependent function}
1381 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1382 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1383 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1384 \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1390 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1391 \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1394 \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1395 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1396 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1401 Dependent functions are one of the two key features that characterise
1402 dependent types---the other being dependent products. With dependent
1403 functions, the result type can depend on the value of the argument.
1404 This feature, together with the fact that the result type might be a
1405 type itself, brings a lot of interesting possibilities. In the
1406 introduction rule, the return type is type checked in a context with an
1407 abstracted variable of domain's type; and in the elimination rule the
1408 actual argument is substituted in the return type. Keeping the
1409 correspondence with logic alive, dependent functions are much like
1410 universal quantifiers ($\forall$) in logic.
1412 For example, assuming that we have lists and natural numbers in our
1413 language, using dependent functions we can write functions of
1417 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1418 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1419 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1420 \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{$>$}} 0 \myarr
1425 \myfun{length} is the usual polymorphic length
1426 function. $\myarg\myfun{$>$}\myarg$ is a function that takes two naturals
1427 and returns a type: if the lhs is greater then the rhs, $\myunit$ is
1428 returned, $\myempty$ otherwise. This way, we can express a
1429 `non-emptiness' condition in $\myfun{head}$, by including a proof that
1430 the length of the list argument is non-zero. This allows us to rule out
1431 the `empty list' case, so that we can safely return the first element.
1433 Finally, we need to make sure that the type hierarchy is respected, which
1434 is the reason why a type formed by $\myarr$ will live in the least upper
1435 bound of the levels of argument and return type.
1437 \subsubsection{$\myprod$, or dependent product}
1440 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1441 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1442 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1443 \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1449 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1450 \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1451 \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1453 \UnaryInfC{\phantom{$--$}}
1456 \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1457 \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1459 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1464 If dependent functions are a generalisation of $\myarr$ in the STLC,
1465 dependent products are a generalisation of $\myprod$ in the STLC. The
1466 improvement is that the second element's type can depend on the value of
1467 the first element. The correspondence with logic is through the
1468 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1469 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1470 The first element will be a number, and the second evidence that the
1471 number is even. This highlights the fact that we are working in a
1472 constructive logic: if we have an existence proof, we can always ask for
1473 a witness. This means, for instance, that $\neg \forall \neg$ is not
1474 equivalent to $\exists$. Additionally, we need to specify the type of
1475 the second element (ranging over the first element) explicitly when
1476 using $\mypair{\myarg}{\myarg}$.
1478 Another perhaps more `dependent' application of products, paired with
1479 $\mybool$, is to offer choice between different types. For example we
1480 can easily recover disjunctions:
1483 \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1484 \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1485 \myfun{case} : (\myb{A}\ \myb{B}\ \myb{C} {:} \mytyp_0) \myarr (\myb{A} \myarr \myb{C}) \myarr (\myb{B} \myarr \myb{C}) \myarr \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \myarr \myb{C} \\
1486 \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1487 \myind{2} \myapp{(\myitee{\myapp{\myfst}{\myb{x}}}{\myb{b}}{(\myite{\myb{b}}{\myb{A}}{\myb{B}})}{\myb{f}}{\myb{g}})}{(\myapp{\mysnd}{\myb{x}})}
1491 \subsubsection{$\mytyc{W}$, or well-order}
1492 \label{sec:well-order}
1494 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1496 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1497 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1498 \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1503 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1504 \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1505 \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1511 \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1512 \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1514 \BinaryInfC{$\myjud{\myse{p}}{
1515 \myfora{\myb{s}}{\myse{S}}{\myfora{\myb{f}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}} \myarr \myw{\myb{x}}{\myse{S}}{\myse{T}}}{(\myfora{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myb{s}}}{\mysub{\myse{P}}{\myb{w}}{\myapp{\myb{f}}{\myb{t}}}}) \myarr \mysub{\myse{P}}{\myb{w}}{\myb{f}}}}
1517 \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1521 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1522 let us represent inductive data in a general way. We can form `nodes'
1523 of the shape \[\mytmt \mynode{\myb{x}}{\mytyb} \myse{f} :
1524 \myw{\myb{x}}{\mytya}{\mytyb}\] where $\mytmt$ is of type $\mytya$ and
1525 is the data present in the node, and $\myse{f}$ specifies a `child' of
1526 the node for each member of $\mysub{\mytyb}{\myb{x}}{\mytmt}$. The
1527 $\myfun{rec}\ \myfun{with}$ acts as an induction principle on
1528 $\mytyc{W}$, given a predicate and a function dealing with the inductive
1529 case---we will gain more intuition about inductive data in Section
1530 \ref{sec:user-type}.
1532 For example, if we want to form natural numbers, we can take
1535 \mytyc{Tr} : \mybool \myarr \mytyp_0 \\
1536 \mytyc{Tr} \myappsp \myb{b} \mapsto \myfun{if}\, \myb{b}\, \myfun{then}\, \myunit\, \myfun{else}\, \myempty \\
1538 \mynat : \mytyp_0 \\
1539 \mynat \mapsto \myw{\myb{b}}{\mybool}{(\mytyc{Tr}\myappsp\myb{b})}
1542 Each node will contain a boolean. If $\mytrue$, the number is non-zero,
1543 and we will have one child representing its predecessor, given that
1544 $\mytyc{Tr}$ will return $\myunit$. If $\myfalse$, the number is zero,
1545 and we will have no predecessors (children), given the $\myempty$:
1548 \mydc{zero} : \mynat \\
1549 \mydc{zero} \mapsto \myfalse \mynodee (\myabs{\myb{x}}{\myabsurd{\mynat} \myappsp \myb{x}}) \\
1551 \mydc{suc} : \mynat \myarr \mynat \\
1552 \mydc{suc}\myappsp \myb{x} \mapsto \mytrue \mynodee (\myabs{\myarg}{\myb{x}})
1555 And with a bit of effort, we can recover addition:
1558 \myfun{plus} : \mynat \myarr \mynat \myarr \mynat \\
1559 \myfun{plus} \myappsp \myb{x} \myappsp \myb{y} \mapsto \\
1560 \myind{2} \myfun{rec}\, \myb{x} / \myb{b}.\mynat \, \\
1561 \myind{2} \myfun{with}\, \myabs{\myb{b}}{\\
1562 \myind{2}\myind{2}\myfun{if}\, \myb{b} / \myb{b'}.((\mytyc{Tr} \myappsp \myb{b'} \myarr \mynat) \myarr (\mytyc{Tr} \myappsp \myb{b'} \myarr \mynat) \myarr \mynat) \\
1563 \myind{2}\myind{2}\myfun{then}\,(\myabs{\myarg\, \myb{f}}{\mydc{suc}\myappsp (\myapp{\myb{f}}{\mytt})})\, \myfun{else}\, (\myabs{\myarg\, \myarg}{\myb{y}})}
1566 Note how we explicitly have to type the branches to make them match
1567 with the definition of $\mynat$. This gives a taste of the clumsiness
1568 of $\mytyc{W}$-types but not the whole story. Well-orders are
1569 inadequate not only because they are verbose, but also because they
1570 face deeper problems due to the weakness of the notion of equality
1571 present in most type theories, which we will present in the next
1572 section \citep{dybjer1997representing}. The `better' equality we will
1573 present in Section \ref{sec:ott} helps but does not fully resolve
1574 these issues.\footnote{See \url{http://www.e-pig.org/epilogue/?p=324},
1575 which concludes with `W-types are a powerful conceptual tool, but
1576 they’re no basis for an implementation of recursive data types in
1577 decidable type theories.'} For this reasons \mytyc{W}-types have
1578 remained nothing more than a reasoning tool, and practical systems
1579 must implement more expressive ways to represent data.
1581 \section{The struggle for equality}
1582 \label{sec:equality}
1584 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1585 schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1587 In the previous section we learnt how a type checker for ITT needs
1588 a notion of \emph{definitional equality}. Beyond this meta-theoretic
1589 notion, in this section we will explore the ways of expressing equality
1590 \emph{inside} the theory, as a reasoning tool available to the user.
1591 This area is the main concern of this thesis, and in general a very
1592 active research topic, since we do not have a fully satisfactory
1593 solution, yet. As in the previous section, everything presented is
1594 formalised in Agda in Appendix \ref{app:agda-itt}.
1596 \subsection{Propositional equality}
1598 \begin{mydef}[Propositional equality] The syntax, reduction, and typing
1599 rules for propositional equality and related constructs are defined
1604 \begin{minipage}{0.5\textwidth}
1607 \begin{array}{r@{\ }c@{\ }l}
1608 \mytmsyn & ::= & \cdots \\
1609 & | & \mypeq \myappsp \mytmsyn \myappsp \mytmsyn \myappsp \mytmsyn \mysynsep
1610 \myapp{\myrefl}{\mytmsyn} \\
1611 & | & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1616 \begin{minipage}{0.5\textwidth}
1617 \mydesc{\phantom{y}reduction:}{\mytmsyn \myred \mytmsyn}{
1619 \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1625 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1626 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1627 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1628 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1629 \TrinaryInfC{$\myjud{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}{\mytyp_l}$}
1635 \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1636 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}$}
1641 \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\mypeq \myappsp \mytya \myappsp \myb{x} \myappsp \myb{y}}{\mytyp_l}}} \\
1642 \myjud{\myse{q}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1645 \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1651 To express equality between two terms inside ITT, the obvious way to do
1652 so is to have equality to be a type. Here we present what has survived
1653 as the dominating form of equality in systems based on ITT up since
1654 \cite{Martin-Lof1984} up to the present day.
1656 Our type former is $\mypeq$, which given a type relates equal terms of
1657 that type. $\mypeq$ has one introduction rule, $\myrefl$, which
1658 introduces an equality relation between definitionally equal terms.
1660 Finally, we have one eliminator for $\mypeq$ , $\myjeqq$ (also known as
1661 `\myfun{J} axiom' in the literature).
1662 $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1664 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1665 $\mytya$) and a proof of their equality;
1666 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1667 $\myse{n}$) are equal;
1668 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$
1669 twice, plus the trivial proof by reflexivity showing that $\myse{m}$
1672 Given these ingredients, $\myjeqq$ returns a member of $\myse{P}$
1673 applied to $\mytmm$, $\mytmn$, and $\myse{q}$. In other words $\myjeqq$
1674 takes a witness that $\myse{P}$ works with \emph{definitionally equal}
1675 terms, and returns a witness of $\myse{P}$ working with
1676 \emph{propositionally equal} terms. Given its reduction rules,
1677 invocations of $\myjeqq$ will vanish when the equality proofs will
1678 reduce to invocations to reflexivity, at which point the arguments must
1679 be definitionally equal, and thus the provided
1680 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1681 can be returned. This means that $\myjeqq$ will not compute with
1682 hypothetical proofs, which makes sense given that they might be false.
1684 While the $\myjeqq$ rule is slightly convoluted, we can derive many more
1685 `friendly' rules from it, for example a more obvious `substitution' rule, that
1686 replaces equal for equal in predicates:
1689 \myfun{subst} : \myfora{\myb{A}}{\mytyp}{\myfora{\myb{P}}{\myb{A} \myarr \mytyp}{\myfora{\myb{x}\ \myb{y}}{\myb{A}}{\mypeq \myappsp \myb{A} \myappsp \myb{x} \myappsp \myb{y} \myarr \myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{\myb{y}}}}} \\
1690 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1691 \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1694 Once we have $\myfun{subst}$, we can easily prove more familiar laws
1695 regarding equality, such as symmetry, transitivity, congruence laws,
1696 etc.\footnote{For definitions of these functions, refer to Appendix \ref{app:itt-code}.}
1698 \subsection{Common extensions}
1700 Our definitional and propositional equalities can be enhanced in various
1701 ways. Obviously if we extend the definitional equality we are also
1702 automatically extend propositional equality, given how $\myrefl$ works.
1704 \subsubsection{$\eta$-expansion}
1705 \label{sec:eta-expand}
1707 A simple extension to our definitional equality is achieved by $\eta$-expansion.
1708 Given an abstract variable $\myb{f} : \mytya \myarr \mytyb$ the aim is
1709 to have that $\myb{f} \mydefeq
1710 \myabss{\myb{x}}{\mytya}{\myapp{\myb{f}}{\myb{x}}}$. We can achieve
1711 this by `expanding' terms depending on their types, a process known as
1712 \emph{quotation}---a term borrowed from the practice of
1713 \emph{normalisation by evaluation}, where we embed terms in some host
1714 language with an existing notion of computation, and then reify them
1715 back into terms, which will `smooth out' differences like the one above
1718 The same concept applies to $\myprod$, where we expand each inhabitant
1719 reconstructing it by getting its projections, so that $\myb{x}
1720 \mydefeq \mypair{\myfst \myappsp \myb{x}}{\mysnd \myappsp \myb{x}}$.
1721 Similarly, all one inhabitants of $\myunit$ and all zero inhabitants of
1722 $\myempty$ can be considered equal. Quotation can be performed in a
1723 type-directed way, as we will witness in Section \ref{sec:kant-irr}.
1725 \begin{mydef}[Congruence and $\eta$-laws]
1726 To justify quotation in our type system we add a congruence law for
1727 abstractions and a similar law for products, plus the fact that all
1728 elements of $\myunit$ or $\myempty$ are equal.
1731 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1733 \AxiomC{$\myjudd{\myctx; \myb{y} : \mytya}{\myapp{\myse{f}}{\myb{x}} \mydefeq \myapp{\myse{g}}{\myb{x}}}{\mysub{\mytyb}{\myb{x}}{\myb{y}}}$}
1734 \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1737 \AxiomC{$\myjud{\mypair{\myapp{\myfst}{\mytmm}}{\myapp{\mysnd}{\mytmm}} \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1738 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1745 \AxiomC{$\myjud{\mytmm}{\myunit}$}
1746 \AxiomC{$\myjud{\mytmn}{\myunit}$}
1747 \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1750 \AxiomC{$\myjud{\mytmm}{\myempty}$}
1751 \AxiomC{$\myjud{\mytmn}{\myempty}$}
1752 \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myempty}$}
1757 \subsubsection{Uniqueness of identity proofs}
1759 Another common but controversial addition to propositional equality is
1760 the $\myfun{K}$ axiom, which essentially states that all equality proofs
1763 \begin{mydef}[$\myfun{K}$ axiom]\end{mydef}
1764 \mydesc{typing:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1767 \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\mypeq \myappsp \mytya \myappsp \myb{x}\myappsp \myb{x} \myarr \mytyp}} \\\
1768 \myjud{\mytmt}{\mytya} \hspace{1cm}
1769 \myjud{\myse{p}}{\myse{P} \myappsp \mytmt \myappsp (\myrefl \myappsp \mytmt)} \hspace{1cm}
1770 \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1773 \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{t} \myappsp \myse{p} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1777 \cite{Hofmann1994} showed that $\myfun{K}$ is not derivable from
1778 $\myjeqq$, and \cite{McBride2004} showed that it is needed to implement
1779 `dependent pattern matching', as first proposed by \cite{Coquand1992}.\footnote{See Section \ref{sec:future-work} for more on dependent pattern matching.}
1780 Thus, $\myfun{K}$ is derivable in the systems that implement dependent
1781 pattern matching, such as Epigram and Agda; but for example not in Coq.
1783 $\myfun{K}$ is controversial mainly because it is at odds with
1784 equalities that include computational behaviour, most notably
1785 Voevodsky's \emph{Univalent Foundations}, which feature a \emph{univalence}
1786 axiom that identifies isomorphisms between types with propositional
1787 equality. For example we would have two isomorphisms, and thus two
1788 equalities, between $\mybool$ and $\mybool$, corresponding to the two
1789 permutations---one is the identity, and one swaps the elements. Given
1790 this, $\myfun{K}$ and univalence are inconsistent, and thus a form of
1791 dependent pattern matching that does not imply $\myfun{K}$ is subject of
1792 research.\footnote{More information about univalence can be found at
1793 \url{http://www.math.ias.edu/~vladimir/Site3/Univalent_Foundations.html}.}
1795 \subsection{Limitations}
1797 Propositional equality as described is quite restricted when
1798 reasoning about equality beyond the term structure, which is what definitional
1799 equality gives us (extensions notwithstanding).
1801 \begin{mydef}[Extensional equality]
1802 Given two functions $\myse{f}$ and $\myse{g}$ of type $\mytya \myarr \mytyb$, they are are said to be \emph{extensionally equal} if
1803 \[ (\myb{x} {:} \mytya) \myarr \mypeq \myappsp \mytyb \myappsp (\myse{f} \myappsp \myb{x}) \myappsp (\myse{g} \myappsp \myb{x}) \]
1806 The problem is best exemplified by \emph{function extensionality}. In
1807 mathematics, we would expect to be able to treat functions that give
1808 equal output for equal input as equal. When reasoning in a mechanised
1809 framework we ought to be able to do the same: in the end, without
1810 considering the operational behaviour, all functions equal extensionally
1811 are going to be replaceable with one another.
1813 However this is not the case, or in other words with the tools we have there is no closed term of type
1815 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1816 \myb{A} \myarr \myb{B}}{
1817 (\myfora{\myb{x}}{\myb{A}}{\mypeq \myappsp \myb{B} \myappsp (\myapp{\myb{f}}{\myb{x}}) \myappsp (\myapp{\myb{g}}{\myb{x}})}) \myarr
1818 \mypeq \myappsp (\myb{A} \myarr \myb{B}) \myappsp \myb{f} \myappsp \myb{g}
1822 To see why this is the case, consider the functions
1823 \[\myabs{\myb{x}}{0 \mathrel{\myfun{$+$}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 0}\]
1824 where $\myfun{$+$}$ is defined by recursion on the first argument,
1825 gradually destructing it to build up successors of the second argument.
1826 The two functions are clearly extensionally equal, and we can in fact
1829 \myfora{\myb{x}}{\mynat}{\mypeq \myappsp \mynat \myappsp (0 \mathrel{\myfun{$+$}} \myb{x}) \myappsp (\myb{x} \mathrel{\myfun{$+$}} 0)}
1831 By induction on $\mynat$ applied to $\myb{x}$. However, the two
1832 functions are not definitionally equal, and thus we will not be able to get
1833 rid of the quantification.
1835 For the reasons given above, theories that offer a propositional equality
1836 similar to what we presented are called \emph{intensional}, as opposed
1837 to \emph{extensional}. Most systems widely used today (such as Agda,
1838 Coq, and Epigram) are of the former kind.
1840 This is quite an annoyance that often makes reasoning awkward or
1841 impossible to execute. For example, we might want to represent terms of
1842 some language in Agda and give their denotation by embedding them in
1843 Agda---if we had $\lambda$-terms, functions will be Agda functions,
1844 application will be Agda's function application, and so on. Then we
1845 would like to perform optimisation passes on the terms, and verify that
1846 they are sound by proving that the denotation of the optimised version
1847 is equal to the denotation of the starting term.
1849 But if the embedding uses functions---and it probably will---we are
1850 stuck with an equality that identifies as equal only syntactically equal
1851 functions! Since the point of optimising is about preserving the
1852 denotational but changing the operational behaviour of terms, our
1853 equality falls short of our needs. Moreover, the problem extends to
1854 other fields beyond functions, such as bisimulation between processes
1855 specified by coinduction, or in general proving equivalences based on
1856 the behaviour of a term.
1858 \subsection{Equality reflection}
1860 One way to `solve' this problem is by identifying propositional equality
1861 with definitional equality.
1863 \begin{mydef}[Equality reflection]\end{mydef}
1864 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1865 \AxiomC{$\myjud{\myse{q}}{\mypeq \myappsp \mytya \myappsp \mytmm \myappsp \mytmn}$}
1866 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1870 The \emph{equality reflection} rule is a very different rule from the
1871 ones we saw up to now: it links a typing judgement internal to the type
1872 theory to a meta-theoretic judgement that the type checker uses to work
1873 with terms. It is easy to see the dangerous consequences that this
1876 \item The rule is not syntax directed, and the type checker is
1877 presumably expected to come up with equality proofs when needed.
1878 \item More worryingly, type checking becomes undecidable also because
1879 computing under false assumptions becomes unsafe, since we derive any
1880 equality proof and then use equality reflection and the conversion
1881 rule to have terms of any type.
1884 Given these facts theories employing equality reflection, like NuPRL
1885 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1886 to keep the systems manageable.
1888 For all its faults, equality reflection does allow us to prove extensionality,
1889 using the extensions we gave above. Assuming that $\myctx$ contains
1890 \[\myb{A}, \myb{B} : \mytyp; \myb{f}, \myb{g} : \myb{A} \myarr \myb{B}; \myb{q} : \myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\]
1894 \AxiomC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myb{q}}{\mypeq \myappsp \myb{A} \myappsp (\myapp{\myb{f}}{\myb{x}}) \myappsp (\myapp{\myb{g}}{\myb{x}})}$}
1895 \RightLabel{equality reflection}
1896 \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1897 \RightLabel{congruence for $\lambda$s}
1898 \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1899 \RightLabel{$\eta$-law for $\lambda$}
1900 \UnaryInfC{$\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}$}
1901 \RightLabel{$\myrefl$}
1902 \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\mypeq \myappsp (\myb{A} \myarr \myb{B}) \myappsp \myb{f} \myappsp \myb{g}}$}
1904 For this reason, theories employing equality reflection are often
1905 grouped under the name of \emph{Extensional Type Theory} (ETT). Now,
1906 the question is: do we need to give up well-behavedness of our theory to
1907 gain extensionality?
1909 \section{The observational approach}
1912 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1913 Theory} (OTT), promises to keep the well behavedness of ITT while
1914 being able to gain many useful equality proofs,\footnote{It is suspected
1915 that OTT gains \emph{all} the equality proofs of ETT, but no proof
1916 exists yet.} including function extensionality. The main idea is to
1917 give the user the possibility to \emph{coerce} (or transport) values
1918 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1919 structurally that $\mytya$ and $\mytyb$ are equal; and providing a
1920 value-level equality based on similar principles. Here we give an
1921 exposition which follows closely the original paper.
1923 \subsection{A simpler theory, a propositional fragment}
1925 \begin{mydef}[OTT's simple theory, with propositions]\ \end{mydef}
1928 $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1930 \begin{array}{r@{\ }c@{\ }l}
1931 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1932 \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1933 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1934 \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1941 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1943 \begin{array}{l@{}l@{\ }c@{\ }l}
1944 \myITE{\mytrue &}{\mytya}{\mytyb} & \myred & \mytya \\
1945 \myITE{\myfalse &}{\mytya}{\mytyb} & \myred & \mytyb
1952 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1954 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1955 \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1958 \AxiomC{$\myjud{\mytmt}{\mybool}$}
1959 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1960 \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1961 \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1968 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1969 \begin{tabular}{ccc}
1970 \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1971 \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1973 \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1976 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1977 \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1978 \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1980 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1983 \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1984 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1985 \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1987 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1992 Our foundation will be a type theory like the one of Section
1993 \ref{sec:itt}, with only one level: $\mytyp_0$. In this context we will
1994 drop the $0$ and call $\mytyp_0$ $\mytyp$. Moreover, since the old
1995 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1996 types thanks to the hierarchy (which is gone), we need to reintroduce an
1997 ad-hoc conditional for types, where the reduction rule is the obvious
2000 However, we have an addition: a universe of \emph{propositions},
2001 $\myprop$.\footnote{Note that we do not need syntax for the type of props, $\myprop$, since the user cannot abstract over them. In fact, we do not not need syntax for $\mytyp$ either, for the same reason.} $\myprop$ isolates a fragment of types at large, and
2002 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$.
2003 \begin{mydef}[Proposition decoding]\ \end{mydef}
2004 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2007 \begin{array}{l@{\ }c@{\ }l}
2008 \myprdec{\mybot} & \myred & \myempty \\
2009 \myprdec{\mytop} & \myred & \myunit
2014 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2015 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2016 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2017 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2022 Propositions are what we call the types of \emph{proofs}, or types
2023 whose inhabitants contain no `data', much like $\myunit$. The goal
2024 when isolating \mytyc{Prop} is twofold: erasing all top-level
2025 propositions when compiling; and identifying all equivalent
2026 propositions as the same, as we will see later.
2028 Why did we choose what we have in $\myprop$? Given the above
2029 criteria, $\mytop$ obviously fits the bill, since it has one element.
2030 A pair of propositions $\myse{P} \myand \myse{Q}$ still won't get us
2031 data, since if they both have one element the only possible pair is
2032 the one formed by said elements. Finally, if $\myse{P}$ is a
2033 proposition and we have $\myprfora{\myb{x}}{\mytya}{\myse{P}}$, the
2034 decoding will be a constant function for propositional content. The
2035 only threat is $\mybot$, by which we can fabricate anything we want:
2036 however if we are consistent there will be no closed term of type
2037 $\mybot$ at, which is enough regarding proof erasure and
2040 As an example of types that are \emph{not} propositional, consider
2041 $\mydc{Bool}$eans, which are the quintessential `relevant' data, since
2042 they are often used to decide the execution path of a program through
2043 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}\myarg$ constructs.
2045 \subsection{Equality proofs}
2047 \begin{mydef}[Equality proofs and related operations]\ \end{mydef}
2051 \begin{array}{r@{\ }c@{\ }l}
2052 \mytmsyn & ::= & \cdots \mysynsep
2053 \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
2054 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2055 \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
2056 \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
2061 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
2063 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2064 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2065 \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
2068 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
2069 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2070 \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
2076 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
2081 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
2084 \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
2089 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
2090 \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
2093 \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
2100 While isolating a propositional universe as presented can be a useful
2101 exercises on its own, what we are really after is a useful notion of
2102 equality. In OTT we want to maintain that things judged to be equal are
2103 still always replaceable for one another with no additional
2104 changes. Note that this is not the same as saying that they are
2105 definitionally equal, since as we saw extensionally equal functions,
2106 while satisfying the above requirement, are not.
2108 Towards this goal we introduce two equality constructs in
2109 $\myprop$---the fact that they are in $\myprop$ indicates that they
2110 indeed have no computational content. The first construct, $\myarg
2111 \myeq \myarg$, relates types, the second,
2112 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values. The
2113 value-level equality is different from our old propositional equality:
2114 instead of ranging over only one type, we might form equalities between
2115 values of different types---the usefulness of this construct will be
2116 clear soon. In the literature this equality is known as `heterogeneous'
2117 or `John Major', since
2120 John Major's `classless society' widened people's aspirations to
2121 equality, but also the gap between rich and poor. After all, aspiring
2122 to be equal to others than oneself is the politics of envy. In much
2123 the same way, forms equations between members of any type, but they
2124 cannot be treated as equals (ie substituted) unless they are of the
2125 same type. Just as before, each thing is only equal to
2126 itself. \citep{McBride1999}.
2129 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
2130 transport values between equal types; and $\myfun{coh}$ (`coherence')
2131 guarantees that $\myfun{coe}$ respects the value-level equality, or in
2132 other words that it really has no computational component: if we
2133 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
2134 will still be the same.
2136 Before introducing the core ideas that make OTT work, let us distinguish
2137 between \emph{canonical} and \emph{neutral} terms and types.
2139 \begin{mydef}[Canonical and neutral types and terms]
2140 In a type theory, \emph{neutral} terms are those formed by an
2141 abstracted variable or by an eliminator (including function
2142 application). Everything else is \emph{canonical}.
2144 In the current system, data constructors ($\mytt$, $\mytrue$,
2145 $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$, ...) will be
2146 canonical, the rest neutral. Correspondingly, canonical types are
2147 those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
2148 and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$).
2149 Neutral types are those formed by
2150 $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
2152 \begin{mydef}[Canonicity]
2153 If in a system all canonical types are inhabited by canonical terms
2154 the system is said to have the \emph{canonicity} property.
2156 The current system, and well-behaved systems in general, has the
2157 canonicity property. Another consequence of normalisation is that all
2158 closed terms will reduce to a canonical term.
2160 \subsubsection{Type equality, and coercions}
2162 The plan is to decompose type-level equalities between canonical types
2163 into decodable propositions containing equalities regarding the
2164 subterms. So if are equating two product types, the equality will
2165 reduce to two subequalities regarding the first and second type. Then,
2166 we can \myfun{coe}rce to transport values between equal types.
2167 Following the subequalities, \myfun{coe} will procede recursively on the
2170 This interplay between the canonicity of equated types, type
2171 equalities, and \myfun{coe}, ensures that invocations of $\myfun{coe}$
2172 will vanish when we have evidence of the structural equality of the
2173 types we are transporting terms across. If the type is neutral, the
2174 equality will not reduce and thus $\myfun{coe}$ will not reduce either.
2175 If we come across an equality between different canonical types, then we
2176 reduce the equality to bottom, making sure that no such proof can exist,
2177 and providing an `escape hatch' in $\myfun{coe}$.
2181 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
2183 \begin{array}{c@{\ }c@{\ }c@{\ }l}
2184 \myempty & \myeq & \myempty & \myred \mytop \\
2185 \myunit & \myeq & \myunit & \myred \mytop \\
2186 \mybool & \myeq & \mybool & \myred \mytop \\
2187 \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
2189 \myind{2} \mytya_1 \myeq \mytya_2 \myand
2190 \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]}
2192 \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
2193 \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
2194 \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
2199 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
2201 \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
2202 \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
2203 \mycoe & \myunit & \myunit & \myse{Q} & \myse{t} & \myred & \mytt \\
2204 \mycoe & \mybool & \mybool & \myse{Q} & \mytrue & \myred & \mytrue \\
2205 \mycoe & \mybool & \mybool & \myse{Q} & \myfalse & \myred & \myfalse \\
2206 \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2207 (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2208 \mytmt_1 & \myred & \\
2210 \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
2211 \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
2212 & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
2213 & \myb{Q_A} & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
2214 & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
2215 & \myb{Q_B} & \mapsto & (\myapp{\mysnd}{\myse{Q}}) \myappsp \myb{\mytmm_1} \myappsp \myb{\mytmm_2} \myappsp (\mycohh{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}}) : \myprdec{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \myeq \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}} \\
2216 & \myb{\mytmn_2} & \mapsto & \mycoee{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}}}{\mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}}{\myb{Q_B}}{\myb{\mytmn_1}} : \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}} \\
2217 \mysyn{in} & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
2220 \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2221 (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2225 \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
2226 (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
2230 \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred & \myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
2234 \caption{Reducing type equalities, and using them when
2235 $\myfun{coe}$rcing.}
2239 \begin{mydef}[Type equalities reduction, and \myfun{coe}rcions] Figure
2240 \ref{fig:eqred} illustrates the rules to reduce equalities and to
2241 coerce terms. We use a $\mysyn{let}$ syntax for legibility.
2243 For ground types, the proof is the trivial element, and \myfun{coe} is
2244 the identity. For $\myunit$, we can do better: we return its only
2245 member without matching on the term. For the three type binders the
2246 choices we make in the type equality are dictated by the desire of
2247 writing the $\myfun{coe}$ in a natural way.
2249 $\myprod$ is the easiest case: we decompose the proof into proofs that
2250 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
2251 proof that given equal values in the first element, the types of the
2252 second elements are equal too
2253 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
2254 \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]}$).\footnote{We
2255 are using $\myimpl$ to indicate a $\forall$ where we discard the
2256 quantified value. We write $\mytyb_1[\myb{x_1}]$ to indicate that the
2257 $\myb{x_1}$ in $\mytyb_1$ is re-bound to the $\myb{x_1}$ quantified by
2258 the $\forall$, and similarly for $\myb{x_2}$ and $\mytyb_2$.} This
2259 also explains the need for heterogeneous equality, since in the second
2260 proof we need to equate terms of possibly different types. In the
2261 respective $\myfun{coe}$ case, since the types are canonical, we know at
2262 this point that the proof of equality is a pair of the shape described
2263 above. Thus, we can immediately coerce the first element of the pair
2264 using the first element of the proof, and then instantiate the second
2265 element with the two first elements and a proof by coherence of their
2266 equality, since we know that the types are equal.
2268 The cases for the other binders are omitted for brevity, but they follow
2269 the same principle with some twists to make $\myfun{coe}$ work with the
2270 generated proofs; the reader can refer to the paper for details.
2272 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
2275 It is important to notice that in the reduction rules for $\myfun{coe}$
2276 are never obstructed by the structure of the proofs. With the exception
2277 of comparisons between different canonical types we never `pattern
2278 match' on the proof pairs, but always look at the projections. This
2279 means that, as long as we are consistent, and thus as long as we don't
2280 have $\mybot$-inducing proofs, we can add propositional axioms for
2281 equality and $\myfun{coe}$ will still compute. Thus, we can take
2282 $\myfun{coh}$ as axiomatic, and we can add back familiar useful equality
2285 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
2286 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2287 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mytmt}{\mytya}}}$}
2292 \AxiomC{$\myjud{\mytya}{\mytyp}$}
2293 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
2294 \BinaryInfC{$\myjud{\mytyc{R} \myappsp (\myb{x} {:} \mytya) \myappsp \mytyb}{\myfora{\myb{y}\, \myb{z}}{\mytya}{\myprdec{\myjm{\myb{y}}{\mytya}{\myb{z}}{\mytya} \myimpl \mysub{\mytyb}{\myb{x}}{\myb{y}} \myeq \mysub{\mytyb}{\myb{x}}{\myb{z}}}}}$}
2298 $\myrefl$ is the equivalent of the reflexivity rule in propositional
2299 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
2300 abstracting over a value we can substitute equal for equal---this lets
2301 us recover $\myfun{subst}$. Note that while we need to provide ad-hoc
2302 rules in the restricted, non-hierarchical theory that we have, if our
2303 theory supports abstraction over $\mytyp$s we can easily add these
2304 axioms as top-level abstracted variables.
2306 \subsubsection{Value-level equality}
2308 \begin{mydef}[Value-level equality]\ \end{mydef}
2310 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
2312 \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
2313 (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
2314 (&\mytmt_1 & : & \myunit&) & \myeq & (&\mytmt_2 & : & \myunit&) & \myred \mytop \\
2315 (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
2316 (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
2317 (&\mytrue & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mybot \\
2318 (&\myfalse & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mybot \\
2319 (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
2320 & \multicolumn{11}{@{}l}{
2321 \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
2322 \myjm{\myapp{\mysnd}{\mytmt_1}}{\mysub{\mytyb_1}{\myb{x_1}}{\myapp{\myfst}{\mytmt_1}}}{\myapp{\mysnd}{\mytmt_2}}{\mysub{\mytyb_2}{\myb{x_2}}{\myapp{\myfst}{\mytmt_2}}}
2324 (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
2325 & \multicolumn{11}{@{}l}{
2326 \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
2327 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
2328 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
2331 (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \cdots \\
2332 (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
2337 As with type-level equality, we want value-level equality to reduce
2338 based on the structure of the compared terms. When matching
2339 propositional data, such as $\myempty$ and $\myunit$, we automatically
2340 return the trivial type, since if a type has zero one members, all
2341 members will be equal. When matching on data-bearing types, such as
2342 $\mybool$, we check that such data matches, and return bottom otherwise.
2343 When matching on records and functions, we rebuild the records to
2344 achieve $\eta$-expansion, and relate functions if they are extensionally
2345 equal---exactly what we wanted. The case for \mytyc{W} is omitted but
2346 unsurprising, it checks that equal data in the nodes will bring equal
2349 \subsection{Proof irrelevance and stuck coercions}
2350 \label{sec:ott-quot}
2352 The last effort is required to make sure that proofs (members of
2353 $\myprop$) are \emph{irrelevant}. Since they are devoid of
2354 computational content, we would like to identify all equivalent
2355 propositions as the same, in a similar way as we identified all
2356 $\myempty$ and all $\myunit$ as the same in section
2357 \ref{sec:eta-expand}.
2359 Thus we will have a quotation that will not only perform
2360 $\eta$-expansion, but will also identify and mark proofs that could not
2361 be decoded (that is, equalities on neutral types). Then, when
2362 comparing terms, marked proofs will be considered equal without
2363 analysing their contents, thus gaining irrelevance.
2365 Moreover we can safely advance `stuck' $\myfun{coe}$rcions between
2366 non-canonical but definitionally equal types. Consider for example
2368 \mycoee{(\myITE{\myb{b}}{\mynat}{\mybool})}{(\myITE{\myb{b}}{\mynat}{\mybool})}{\myb{x}}
2370 Where $\myb{b}$ and $\myb{x}$ are abstracted variables. This
2371 $\myfun{coe}$ will not advance, since the types are not canonical.
2372 However they are definitionally equal, and thus we can safely remove the
2373 coerce and return $\myb{x}$ as it is.
2375 \section{\mykant: the theory}
2376 \label{sec:kant-theory}
2378 \epigraph{\emph{The construction itself is an art, its application to the world an evil parasite.}}{Luitzen Egbertus Jan `Bertus' Brouwer}
2380 \mykant\ is an interactive theorem prover developed as part of this thesis.
2381 The plan is to present a core language which would be capable of serving as
2382 the basis for a more featureful system, while still presenting interesting
2383 features and more importantly observational equality.
2385 We will first present the features of the system, along with motivations
2386 and trade-offs for the design decisions made. Then we describe the
2387 implementation we have developed in Section \ref{sec:kant-practice}.
2388 For an overview of the features of \mykant, see
2389 Section \ref{sec:contributions}, here we present them one by one. The
2390 exception is type holes, which we do not describe holes rigorously, but
2391 provide more information about them in Section \ref{sec:type-holes}.
2393 Note that in this section we will present \mykant\ terms in a fancy
2394 \LaTeX\ dress too keep up with the presentation, but every term, with its
2395 syntax reduced to the concrete syntax, is a valid \mykant\ term accepted
2396 by \mykant\ the software, and not only \mykant\ the theory. Appendix
2397 \ref{app:kant-examples} displays most of the terms in this section in
2398 their concrete syntax.
2400 \subsection{Bidirectional type checking}
2402 We start by describing bidirectional type checking since it calls for
2403 fairly different typing rules that what we have seen up to now. The
2404 idea is to have two kinds of terms: terms for which a type can always be
2405 inferred, and terms that need to be checked against a type. A nice
2406 observation is that this duality is in correspondence with the notion of
2407 canonical and neutral terms: neutral terms
2408 (abstracted or defined variables, function application, record
2409 projections, primitive recursors, etc.) \emph{infer} types, canonical
2410 terms (abstractions, record/data types data constructors, etc.) need to
2413 To introduce the concept and notation, we will revisit the STLC in a
2414 bidirectional style. The presentation follows \cite{Loh2010}. The
2415 syntax for our bidirectional STLC is the same as the untyped
2416 $\lambda$-calculus, but with an extra construct to annotate terms
2417 explicitly---this will be necessary when dealing with top-level
2418 canonical terms. The types are the same as those found in the normal
2421 \begin{mydef}[Syntax for the annotated $\lambda$-calculus]\ \end{mydef}
2425 \begin{array}{r@{\ }c@{\ }l}
2426 \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep (\mytmsyn : \mytysyn)
2431 We will have two kinds of typing judgements: \emph{inference} and
2432 \emph{checking}. $\myinf{\mytmt}{\mytya}$ indicates that $\mytmt$
2433 infers the type $\mytya$, while $\mychk{\mytmt}{\mytya}$ can be checked
2434 against type $\mytya$. The arrows signify the direction of the type
2435 checking---inference pushes types up, checking propagates types
2438 The type of variables in context is inferred, and so are annotate terms.
2439 The type of applications is inferred too, propagating types down the
2440 applied term. Abstractions are checked. Finally, we have a rule to
2441 check the type of an inferrable term.
2443 \begin{mydef}[Bidirectional type checking for the STLC]\ \end{mydef}
2445 \mydesc{typing:}{\myctx \vdash \mytmsyn \Updownarrow \mytmsyn}{
2447 \AxiomC{$\myctx(x) = A$}
2448 \UnaryInfC{$\myinf{\myb{x}}{A}$}
2451 \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
2452 \UnaryInfC{$\mychk{\myabs{x}{\mytmt}}{(\myb{x} {:} \mytya) \myarr \mytyb}$}
2458 \begin{tabular}{ccc}
2459 \AxiomC{$\myinf{\mytmm}{\mytya \myarr \mytyb}$}
2460 \AxiomC{$\mychk{\mytmn}{\mytya}$}
2461 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
2464 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2465 \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2468 \AxiomC{$\myinf{\mytmt}{\mytya}$}
2469 \UnaryInfC{$\mychk{\mytmt}{\mytya}$}
2474 For example, if we wanted to type function composition (in this case for
2475 naturals), we would have to annotate the term:
2478 \myfun{comp} : (\mynat \myarr \mynat) \myarr (\mynat \myarr \mynat) \myarr \mynat \myarr \mynat \\
2479 \myfun{comp} \mapsto (\myabs{\myb{f}\, \myb{g}\, \myb{x}}{\myb{f}\myappsp(\myb{g}\myappsp\myb{x})})
2482 But we would not have to annotate functions passed to it, since the type would be propagated to the arguments:
2484 \myfun{comp}\myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$+$}} 3}) \myappsp (\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{$*$}} 4}) \myappsp 42 : \mynat
2487 \subsection{Base terms and types}
2489 Let us begin by describing the primitives available without the user
2490 defining any data types, and without equality. The way we handle
2491 variables and substitution is left unspecified, and explained in section
2492 \ref{sec:term-repr}, along with other implementation issues. We are
2493 also going to give an account of the implicit type hierarchy separately
2494 in Section \ref{sec:term-hierarchy}, so as not to clutter derivation
2495 rules too much, and just treat types as impredicative for the time
2498 \begin{mydef}[Syntax for base types in \mykant]\ \end{mydef}
2502 \begin{array}{r@{\ }c@{\ }l}
2503 \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
2504 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
2505 \myabs{\myb{x}}{\mytmsyn} \mysynsep
2506 (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
2507 (\myann{\mytmsyn}{\mytmsyn}) \\
2508 \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
2513 The syntax for our calculus includes just two basic constructs:
2514 abstractions and $\mytyp$s. Everything else will be user-defined.
2515 Since we let the user define values too, we will need a context capable
2516 of carrying the body of variables along with their type.
2518 \begin{mydef}[Context validity]
2519 Bound names and defined names are treated separately in the syntax, and
2520 while both can be associated to a type in the context, only defined
2521 names can be associated with a body.
2524 \mydesc{context validity:}{\myvalid{\myctx}}{
2525 \begin{tabular}{ccc}
2526 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
2527 \UnaryInfC{$\myvalid{\myemptyctx}$}
2530 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2531 \AxiomC{$\mynamesyn \not\in \myctx$}
2532 \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
2535 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2536 \AxiomC{$\myfun{f} \not\in \myctx$}
2537 \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
2542 Now we can present the reduction rules, which are unsurprising. We have
2543 the usual function application ($\beta$-reduction), but also a rule to
2544 replace names with their bodies ($\delta$-reduction), and one to discard
2545 type annotations. For this reason reduction is done in-context, as
2546 opposed to what we have seen in the past.
2548 \begin{mydef}[Reduction rules for base types in \mykant]\ \end{mydef}
2550 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
2551 \begin{tabular}{ccc}
2552 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2553 \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
2554 \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
2557 \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
2558 \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
2561 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
2562 \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
2567 We can now give types to our terms. Although we include the usual
2568 conversion rule, we defer a detailed account of definitional equality to
2569 Section \ref{sec:kant-irr}.
2571 \begin{mydef}[Bidirectional type checking for base types in \mykant]\ \end{mydef}
2573 \mydesc{typing:}{\myctx \vdash \mytmsyn \Updownarrow \mytmsyn}{
2574 \begin{tabular}{cccc}
2575 \AxiomC{$\myse{name} : A \in \myctx$}
2576 \UnaryInfC{$\myinf{\myse{name}}{A}$}
2579 \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
2580 \UnaryInfC{$\myinf{\myfun{f}}{A}$}
2583 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2584 \UnaryInfC{$\myinf{\myann{\mytmt}{\mytya}}{\mytya}$}
2587 \AxiomC{$\myinf{\mytmt}{\mytya}$}
2588 \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
2589 \BinaryInfC{$\mychk{\mytmt}{\mytyb}$}
2597 \AxiomC{\phantom{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}}
2598 \UnaryInfC{$\myinf{\mytyp}{\mytyp}$}
2601 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2602 \AxiomC{$\mychkk{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
2603 \BinaryInfC{$\myinf{(\myb{x} {:} \mytya) \myarr \mytyb}{\mytyp}$}
2612 \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
2613 \AxiomC{$\mychk{\mytmn}{\mytya}$}
2614 \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
2619 \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
2620 \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2626 \subsection{Elaboration}
2628 As we mentioned, $\mykant$\ allows the user to define not only values
2629 but also custom data types and records. \emph{Elaboration} consists of
2630 turning these declarations into workable syntax, types, and reduction
2631 rules. The treatment of custom types in $\mykant$\ is heavily inspired
2632 by McBride's and McKinna's early work on Epigram \citep{McBride2004},
2633 although with some differences.
2635 \subsubsection{Term vectors, telescopes, and assorted notation}
2637 \begin{mydef}[Term vector]
2638 A \emph{term vector} is a series of terms. The empty vector is
2639 represented by $\myemptyctx$, and a new element is added with
2640 $\myarg;\myarg$, similarly to contexts---$\vec{t};\mytmm$.
2643 We denote term vectors with the usual arrow notation,
2644 e.g. $vec{\mytmt}$, $\myvec{\mytmt};\mytmm$, etc. We often use term
2645 vectors to refer to a series of term applied to another. For example
2646 $\mytyc{D} \myappsp \vec{A}$ is a shorthand for $\mytyc{D} \myappsp
2647 \mytya_1 \cdots \mytya_n$, for some $n$. $n$ is consistently used to
2648 refer to the length of such vectors, and $i$ to refer to an index such
2649 that $1 \le i \le n$.
2651 \begin{mydef}[Telescope]
2652 A \emph{telescope} is a series of typed bindings. The empty telescope
2653 is represented by $\myemptyctx$, and a binding is added via
2657 To present the elaboration and operations on user defined data types, we
2658 frequently make use what \cite{Bruijn91} called \emph{telescopes}, a
2659 construct that will prove useful when dealing with the types of type and
2660 data constructors. We refer to telescopes with $\mytele$, $\mytele'$,
2661 $\mytele_i$, etc. If $\mytele$ refers to a telescope, $\mytelee$ refers
2662 to the term vector made up of all the variables bound by $\mytele$.
2663 $\mytele \myarr \mytya$ refers to the type made by turning the telescope
2664 into a series of $\myarr$. For example we have that
2666 (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2667 (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2670 We make use of various operations to manipulate telescopes:
2672 \item $\myhead(\mytele)$ refers to the first type appearing in
2673 $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2674 \myapp{\myfun{even}}{\myb{x}})) = \mynat$. Similarly,
2675 $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2677 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2678 first $i$ elements of $\mytele$: $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2679 \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$.
2680 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2681 terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2682 \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2683 \myapp{\myfun{even}}{42})$.
2686 Additionally, when presenting syntax elaboration, We use $\mytmsyn^n$ to
2687 indicate a term vector composed of $n$ elements. When clear from the
2688 context, we use term vectors to signify their length,
2689 e.g. $\mytmsyn^{\mytele}$, or $1 \le i \le \mytele$.
2691 \subsubsection{Declarations syntax}
2693 \begin{mydef}[Syntax of declarations in \mykant]\ \end{mydef}
2697 \begin{array}{r@{\ }c@{\ }l}
2698 \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2699 & | & \mypost{\myb{x}}{\mytmsyn} \\
2700 & | & \myadt{\mytyc{D}}{\myappsp \mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2701 & | & \myreco{\mytyc{D}}{\myappsp \mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2703 \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2704 \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2708 In \mykant\ we have four kind of declarations:
2711 \item[Defined value] A variable, together with a type and a body.
2712 \item[Abstract variable] An abstract variable, with a type but no body.
2713 \item[Inductive data] A \emph{data type}, with a \emph{type constructor}
2714 (denoted in blue, capitalised, sans serif: $\mytyc{D}$) various
2715 \emph{data constructors} (denoted in red, lowercase, sans serif:
2716 $\mydc{c}$), quite similar to what we find in Haskell. A primitive
2717 \emph{eliminator} (or \emph{destructor}, or \emph{recursor}; denoted
2718 by green, lowercase, roman: \myfun{elim}) will be used to compute with
2720 \item[Record] A \emph{record}, which like data types consists of a type
2721 constructor but only one data constructor. The user can also define
2722 various \emph{fields}, with no recursive occurrences of the type. The
2723 functions extracting the fields' values from an instance of a record
2724 are called \emph{projections} (denoted in the same way as destructors).
2727 Elaborating defined variables consists of type checking the body against
2728 the given type, and updating the context to contain the new binding.
2729 Elaborating abstract variables and abstract variables consists of type
2730 checking the type, and updating the context with a new typed variable.
2732 \begin{mydef}[Elaboration of defined and abstract variables]\ \end{mydef}
2734 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2736 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2737 \AxiomC{$\myfun{f} \not\in \myctx$}
2739 $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \ \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2743 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2744 \AxiomC{$\myfun{f} \not\in \myctx$}
2747 \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2748 \ \ \myelabf\ \ \myctx; \myfun{f} : \mytya
2755 \subsubsection{User defined types}
2756 \label{sec:user-type}
2758 Elaborating user defined types is the real effort. First, we will
2759 explain what we can define, with some examples.
2762 \item[Natural numbers] To define natural numbers, we create a data type
2763 with two constructors: one with zero arguments ($\mydc{zero}$) and one
2764 with one recursive argument ($\mydc{suc}$):
2767 \myadt{\mynat}{ }{ }{
2768 \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2772 This is very similar to what we would write in Haskell:
2774 data Nat = Zero | Suc Nat
2776 Once the data type is defined, $\mykant$\ will generate syntactic
2777 constructs for the type and data constructors, so that we will have
2780 \begin{tabular}{ccc}
2781 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2782 \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2785 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2786 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2789 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2790 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2794 While in Haskell (or indeed in Agda or Coq) data constructors are
2795 treated the same way as functions, in $\mykant$\ they are syntax, so
2796 for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will give a
2797 syntax error. This is necessary so that we can easily infer the type
2798 of polymorphic data constructors, as we will see later.
2800 Moreover, each data constructor is prefixed by the type constructor
2801 name, since we need to retrieve the type constructor of a data
2802 constructor when type checking. This measure aids in the presentation
2803 of the theory but it is not needed in the implementation, where
2804 we can have a dictionary to look up the type constructor corresponding
2805 to each data constructor. When using data constructors in examples I
2806 will omit the type constructor prefix for brevity, in this case
2807 writing $\mydc{zero}$ instead of $\mynat.\mydc{zero}$ and $\mydc{suc}$ instead of
2808 $\mynat.\mydc{suc}$.
2810 Along with user defined constructors, $\mykant$\ automatically
2811 generates an \emph{eliminator}, or \emph{destructor}, to compute with
2812 natural numbers: If we have $\mytmt : \mynat$, we can destruct
2813 $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2816 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2818 \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2820 \myfora{\myb{P}}{\mynat \myarr \mytyp}{ \\ \myapp{\myb{P}}{\mydc{zero}} \myarr (\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}) \myarr \\ \myapp{\myb{P}}{\mytmt}}
2824 $\mynat.\myfun{elim}$ corresponds to the induction principle for
2825 natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2826 know that predicate holds for the base case
2827 ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2828 ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2829 \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2830 holds for any number. As with the data constructors, we require the
2831 eliminator to be applied to the `destructed' element.
2833 While the induction principle is usually seen as a mean to prove
2834 properties about numbers, in the intuitionistic setting it is also a
2835 mean to compute. In this specific case $\mynat.\myfun{elim}$
2836 returns the base case if the provided number is $\mydc{zero}$, and
2837 recursively applies the inductive step if the number is a
2840 \begin{array}{@{}l@{}l}
2841 \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2842 \mytyc{\mynat}.\myfun{elim} \myappsp (\mydc{suc} \myappsp \mytmt) & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{ps} \myappsp \mytmt \myappsp (\mynat.\myfun{elim} \myappsp \mytmt \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps})
2845 The Haskell equivalent would be
2847 elim :: Nat -> a -> (Nat -> a -> a) -> a
2848 elim Zero pz ps = pz
2849 elim (Suc n) pz ps = ps n (elim n pz ps)
2851 Which buys us the computational behaviour, but not the reasoning power,
2852 since we cannot express the notion of a predicate depending on
2853 $\mynat$---the type system is far too weak.
2855 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2856 lists are too similar to natural numbers to be interesting.
2859 \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2860 \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2864 Now the purpose of `constructors as syntax' can be explained: what would
2865 the type of $\mydc{leaf}$ be? If we were to treat it as a `normal'
2866 term, we would have to specify the type parameter of the tree each
2867 time the constructor is applied:
2869 \begin{array}{@{}l@{\ }l}
2870 \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2871 \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2874 The problem with this approach is that creating terms is incredibly
2875 verbose and dull, since we would need to specify the type parameter of
2876 $\mytyc{Tree}$ each time. For example if we wished to create a
2877 $\mytree \myappsp \mynat$ with two nodes and three leaves, we would
2880 \mydc{node} \myappsp \mynat \myappsp (\mydc{node} \myappsp \mynat \myappsp (\mydc{leaf} \myappsp \mynat) \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp (\mydc{leaf} \myappsp \mynat)) \myappsp \mydc{zero} \myappsp (\mydc{leaf} \myappsp \mynat)
2882 The redundancy of $\mynat$s is quite irritating. Instead, if we treat
2883 constructors as syntactic elements, we can `extract' the type of the
2884 parameter from the type that the term gets checked against, much like
2885 what we do to type abstractions:
2889 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2890 \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2893 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2894 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2895 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2896 \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2900 Which enables us to write, much more concisely
2902 \mydc{node} \myappsp (\mydc{node} \myappsp \mydc{leaf} \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp \mydc{leaf}) \myappsp \mydc{zero} \myappsp \mydc{leaf} : \myapp{\mytree}{\mynat}
2904 We gain an annotation, but we lose the myriad of types applied to the
2905 constructors. Conversely, with the eliminator for $\mytree$, we can
2906 infer the type of the arguments given the type of the destructed:
2909 \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2911 \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2913 (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2914 \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2915 ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2916 \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2917 \myapp{\myb{P}}{\mytmt}
2922 As expected, the eliminator embodies structural induction on trees.
2923 We have a base case for $\myb{P} \myappsp \mydc{leaf}$, and an
2924 inductive step that given two subtrees and the predicate applied to
2925 them needs to return the predicate applied to the tree formed by a
2926 node with the two subtrees as children.
2928 \item[Empty type] We have presented types that have at least one
2929 constructors, but nothing prevents us from defining types with
2930 \emph{no} constructors:
2931 \[\myadt{\mytyc{Empty}}{ }{ }{ }\]
2932 What shall the `induction principle' on $\mytyc{Empty}$ be? Does it
2933 even make sense to talk about induction on $\mytyc{Empty}$?
2934 $\mykant$\ does not care, and generates an eliminator with no `cases':
2937 \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2938 \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2940 which lets us write the $\myfun{absurd}$ that we know and love:
2943 \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \myempty \myarr \myb{A} \\
2944 \myfun{absurd}\myappsp \myb{A} \myappsp \myb{x} \mapsto \myempty.\myfun{elim} \myappsp \myb{x} \myappsp (\myabs{\myarg}{\myb{A}})
2948 \item[Ordered lists] Up to this point, the examples shown are nothing
2949 new to the \{Haskell, SML, OCaml, functional\} programmer. However
2950 dependent types let us express much more than that. A useful example
2951 is the type of ordered lists. There are many ways to define such a
2952 thing, but we will define ours to store the bounds of the list, making
2953 sure that $\mydc{cons}$ing respects that.
2955 First, using $\myunit$ and $\myempty$, we define a type expressing the
2956 ordering on natural numbers, $\myfun{le}$---`less or equal'.
2957 $\myfun{le}\myappsp \mytmm \myappsp \mytmn$ will be inhabited only if
2958 $\mytmm \le \mytmn$:
2961 \myfun{le} : \mynat \myarr \mynat \myarr \mytyp \\
2962 \myfun{le} \myappsp \myb{n} \mapsto \\
2963 \myind{2} \mynat.\myfun{elim} \\
2964 \myind{2}\myind{2} \myb{n} \\
2965 \myind{2}\myind{2} (\myabs{\myarg}{\mynat \myarr \mytyp}) \\
2966 \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2967 \myind{2}\myind{2} (\myabs{\myb{n}\, \myb{f}\, \myb{m}}{
2968 \mynat.\myfun{elim} \myappsp \myb{m} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myb{m'}\, \myarg}{\myapp{\myb{f}}{\myb{m'}}})
2972 We return $\myunit$ if the scrutinised is $\mydc{zero}$ (every
2973 number in less or equal than zero), $\myempty$ if the first number is
2974 a $\mydc{suc}$cessor and the second a $\mydc{zero}$, and we recurse if
2975 they are both successors. Since we want the list to have possibly
2976 `open' bounds, for example for empty lists, we create a type for
2977 `lifted' naturals with a bottom ($\le$ everything but itself) and top
2978 ($\ge$ everything but itself) elements, along with an associated comparison
2982 \myadt{\mytyc{Lift}}{ }{ }{\mydc{bot} \mydcsep \mydc{lift} \myappsp \mynat \mydcsep \mydc{top}}\\
2983 \myfun{le'} : \mytyc{Lift} \myarr \mytyc{Lift} \myarr \mytyp\\
2984 \myfun{le'} \myappsp \myb{l_1} \mapsto \\
2985 \myind{2} \mytyc{Lift}.\myfun{elim} \\
2986 \myind{2}\myind{2} \myb{l_1} \\
2987 \myind{2}\myind{2} (\myabs{\myarg}{\mytyc{Lift} \myarr \mytyp}) \\
2988 \myind{2}\myind{2} (\myabs{\myarg}{\myunit}) \\
2989 \myind{2}\myind{2} (\myabs{\myb{n_1}\, \myb{l_2}}{
2990 \mytyc{Lift}.\myfun{elim} \myappsp \myb{l_2} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myb{n_2}}{\myfun{le} \myappsp \myb{n_1} \myappsp \myb{n_2}}) \myappsp \myunit
2992 \myind{2}\myind{2} (\myabs{\myb{l_2}}{
2993 \mytyc{Lift}.\myfun{elim} \myappsp \myb{l_2} \myappsp (\myabs{\myarg}{\mytyp}) \myappsp \myempty \myappsp (\myabs{\myarg}{\myempty}) \myappsp \myunit
2997 Finally, we can define a type of ordered lists. The type is
2998 parametrised over two \emph{values} representing the lower and upper
2999 bounds of the elements, as opposed to the \emph{type} parameters
3000 that we are used to in Haskell or similar languages. An empty
3001 list will have to have evidence that the bounds are ordered, and
3002 each time we add an element we require the list to have a matching
3006 \myadt{\mytyc{OList}}{\myappsp (\myb{low}\ \myb{upp} {:} \mytyc{Lift})}{\\ \myind{2}}{
3007 \mydc{nil} \myappsp (\myfun{le'} \myappsp \myb{low} \myappsp \myb{upp}) \mydcsep \mydc{cons} \myappsp (\myb{n} {:} \mynat) \myappsp (\mytyc{OList} \myappsp (\myfun{lift} \myappsp \myb{n}) \myappsp \myb{upp}) \myappsp (\myfun{le'} \myappsp \myb{low} \myappsp (\myfun{lift} \myappsp \myb{n})
3011 Note that in the $\mydc{cons}$ constructor we quantify over the first
3012 argument, which will determine the type of the following
3013 arguments---again something we cannot do in systems like Haskell. If
3014 we want we can then employ this structure to write and prove correct
3015 various sorting algorithms.\footnote{See this presentation by Conor
3017 \url{https://personal.cis.strath.ac.uk/conor.mcbride/Pivotal.pdf},
3018 and this blog post by the author:
3019 \url{http://mazzo.li/posts/AgdaSort.html}.}
3021 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
3022 us another way to define types: $\mysyn{record}$. A record is a
3023 data type with one constructor and `projections' to extract specific
3024 fields of the said constructor.
3026 For example, we can recover dependent products:
3029 \myreco{\mytyc{Prod}}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
3032 Here $\myfst$ and $\mysnd$ are the projections, with their respective
3033 types. Note that each field can refer to the preceding fields---in
3034 this case we have the type of $\myfun{snd}$ depending on the value of
3035 $\myfun{fst}$. A constructor will be automatically generated, under
3036 the name of $\mytyc{Prod}.\mydc{constr}$. Dually to data types, we
3037 will omit the type constructor prefix for record projections.
3039 Following the bidirectionality of the system, we have that projections
3040 (the destructors of the record) infer the type, while the constructor
3045 \AxiomC{$\mychk{\mytmm}{\mytya}$}
3046 \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
3047 \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
3049 \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
3052 \AxiomC{$\hspace{0.2cm}\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}\hspace{0.2cm}$}
3053 \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
3055 \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
3059 What we have defined here is equivalent to ITT's dependent products.
3069 \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
3076 \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
3079 \begin{array}{r@{\ }l}
3080 & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
3083 \begin{array}{r@{\ }c@{\ }l}
3084 \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
3085 \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
3093 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
3098 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
3099 \mytyc{D} \not\in \myctx \\
3100 \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
3101 \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
3105 \begin{array}{r@{\ }c@{\ }l}
3106 \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
3107 & & \vspace{-0.2cm} \\
3108 & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
3110 \begin{array}{@{}r@{\ }l l}
3111 \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
3112 & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
3116 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
3117 \end{array} \right \}
3118 & \textbf{methods} \\
3119 & \myapp{\myb{P}}{\myb{x}} &
3123 \DisplayProof \\ \vspace{0.2cm}\ \\
3125 \begin{array}{@{}l l@{\ } l@{} r c l}
3126 \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
3127 & \myhyps(\myb{P}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{\mytmt}}) \mycc \mytele &) & \mymetagoes & (\myb{r'} {:} \myapp{\myb{P}}{\myb{r}}) \mycc \myhyps(\myb{P}, \mytele) \\
3128 & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
3136 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
3138 $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
3139 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3140 \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
3142 \myctx \vdash \myapp{\myapp{\myapp{\mytyc{D}.\myfun{elim}}{(\myapp{\mytyc{D}.\mydc{c}_i}{\vec{\myse{t}}})}}{\myse{P}}}{\vec{\myse{m}}} \myred \myapp{\myapp{\myse{m}_i}{\vec{\mytmt}}}{\myrecs(\myse{P}, \vec{m}, \mytele_i)}
3144 \DisplayProof \\ \vspace{0.2cm}\ \\
3146 \begin{array}{@{}l l@{\ } l@{} r c l}
3147 \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
3148 & \myrecs(\myse{P}, \vec{m}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{A}}); \mytele & ) & \mymetagoes & (\mytyc{D}.\myfun{elim} \myappsp \myb{r} \myappsp \myse{P} \myappsp \vec{m}); \myrecs(\myse{P}, \vec{m}, \mytele) \\
3149 & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
3156 \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
3159 \begin{array}{r@{\ }c@{\ }l}
3160 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
3163 \begin{array}{r@{\ }c@{\ }l}
3164 \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \mytyc{D}.\mydc{constr} \myappsp \mytmsyn^{n} \mysynsep \cdots \mysynsep \mytyc{D}.\myfun{f}_n \myappsp \mytmsyn \\
3172 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
3176 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
3177 \mytyc{D} \not\in \myctx \\
3178 \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
3182 \begin{array}{r@{\ }c@{\ }l}
3183 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
3184 & & \vspace{-0.2cm} \\
3185 & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\myfun{f}_n : \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \mysub{\myse{F}_n}{\myb{f}_i}{\myapp{\myfun{f}_i}{\myb{x}}}_{i = 1}^{n-1}; \\
3186 & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
3194 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
3196 $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
3197 \AxiomC{$\mytyc{D} \in \myctx$}
3198 \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
3202 \caption{Elaboration for data types and records.}
3206 \begin{mydef}[Elaboration for user defined types]
3207 Following the intuition given by the examples, the full elaboration
3208 machinery is presented Figure \ref{fig:elab}.
3210 Our elaboration is essentially a modification of Figure 9 of
3211 \cite{McBride2004}. However, our data types are not inductive
3212 families,\footnote{See Section \ref{sec:future-work} for a brief
3213 description of inductive families.} we do bidirectional type checking
3214 by treating constructors/destructors as syntax, and we have records.
3216 \begin{mydef}[Strict positivity]
3217 A inductive type declaration is \emph{strictly positive} if recursive
3218 occurrences of the type we are defining do not appear embedded
3219 anywhere in the domain part of any function in the types for the data
3222 In data type declarations we allow recursive occurrences as long as they
3223 are strictly positive, which ensures the consistency of the theory. To
3224 achieve that we employing a syntactic check to make sure that this is
3225 the case---in fact the check is stricter than necessary for simplicity,
3226 given that we allow recursive occurrences only at the top level of data
3227 constructor arguments. For example a definition of the $\mytyc{W}$ type
3228 is accepted in Agda but rejected in \mykant. This is to make the
3229 eliminator generation simpler, and in practice it is seldom an
3232 Without these precautions, we can easily derive any type with no
3235 data Fix a = Fix (Fix a -> a) -- Negative occurrence of `Fix a'
3236 -- Term inhabiting any type `a'
3238 boom = (\f -> f (Fix f)) (\x -> (\(Fix f) -> f) x x)
3240 See \cite{Dybjer1991} for a more formal treatment of inductive
3243 For what concerns records, recursive occurrences are disallowed. The
3244 reason for this choice is answered by the reason for the choice of
3245 having records at all: we need records to give the user types with
3246 $\eta$-laws for equality, as we saw in Section \ref{sec:eta-expand}
3247 and in the treatment of OTT in Section \ref{sec:ott}. If we tried to
3248 $\eta$-expand recursive data types, we would expand forever.
3250 \begin{mydef}[Bidirectional type checking for elaborated types]
3251 To implement bidirectional type checking for constructors and
3252 destructors, we store their types in full in the context, and then
3253 instantiate when due.
3256 \mydesc{typing:}{\myctx
3257 \vdash \mytmsyn \Updownarrow \mytmsyn}{ \AxiomC{$
3259 \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
3260 \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
3261 \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
3262 \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
3263 \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \
3264 (1 \le i \le \mytele'')
3267 \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
3272 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3273 \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
3274 \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
3275 \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
3276 \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
3277 \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
3278 \myse{F})(\vec{A};\mytmt)}$}
3281 Note that for 0-ary type constructors, like $\mynat$, we do not need to
3282 check canonical terms: we can automatically infer that $\mydc{zero}$ and
3283 $\mydc{suc}\myappsp n$ are of type $\mynat$. \mykant\ implements this measure, even
3284 if it is not shown in the typing rule for simplicity.
3286 \subsubsection{Why user defined types? Why eliminators?}
3288 The hardest design choice in developing $\mykant$\ was to decide whether
3289 user defined types should be included, and how to handle them. As we
3290 saw, while we can devise general structures like $\mytyc{W}$, they are
3291 unsuitable both for for direct usage and `mechanical' usage. Thus most
3292 theorem provers in the wild provide some means for the user to define
3293 structures tailored to specific uses.
3295 Even if we take user defined types for granted, while there is not much
3296 debate on how to handle records, there are two broad schools of thought
3297 regarding the handling of data types:
3299 \item[Fixed points and pattern matching] The road chosen by Agda and Coq.
3300 Functions are written like in Haskell---matching on the input and with
3301 explicit recursion. An external check on the recursive arguments
3302 ensures that they are decreasing, and thus that all functions
3303 terminate. This approach is the best in terms of user usability, but
3304 it is tricky to implement correctly.
3306 \item[Elaboration into eliminators] The road chose by \mykant, and
3307 pioneered by the Epigram line of work. The advantage is that we can
3308 reduce every data type to simple definitions which guarantee
3309 termination and are simple to reduce and type. It is however more
3310 cumbersome to use than pattern matching, although \cite{McBride2004}
3311 has shown how to implement an expressive pattern matching interface on
3312 top of a larger set of combinators of those provided by \mykant.
3314 We can go ever further down this road and elaborate the declarations
3315 for data types themselves to a small set of primitives, so that our `core'
3316 language will be very small and manageable
3317 \citep{dagand2012elaborating, chapman2010gentle}.
3320 We chose the safer and easier to implement path, given the time
3321 constraints and the higher confidence of correctness. See also Section
3322 \ref{sec:future-work} for a brief overview of ways to extend or treat
3325 \subsection{Cumulative hierarchy and typical ambiguity}
3326 \label{sec:term-hierarchy}
3328 Having a well founded type hierarchy is crucial if we want to retain
3329 consistency, otherwise we can break our type systems by proving bottom,
3330 as shown in Appendix \ref{app:hurkens}.
3332 However, hierarchy as presented in section \ref{sec:itt} is a
3333 considerable burden on the user, on various levels. Consider for
3334 example how we recovered disjunctions in Section \ref{sec:disju}: we
3335 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
3336 What if we wanted to form a disjunction containing something a
3337 $\mytyp_1$, or $\mytyp_{42}$? Our definition would fail us, since
3338 $\mytyp_1 : \mytyp_2$.
3342 \mydesc{cumulativity:}{\myctx \vdash \mytmsyn \mycumul \mytmsyn}{
3343 \begin{tabular}{ccc}
3344 \AxiomC{$\myctx \vdash \mytya \mydefeq \mytyb$}
3345 \UnaryInfC{$\myctx \vdash \mytya \mycumul \mytyb$}
3348 \AxiomC{\phantom{$\myctx \vdash \mytya \mydefeq \mytyb$}}
3349 \UnaryInfC{$\myctx \vdash \mytyp_l \mycumul \mytyp_{l+1}$}
3352 \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3353 \AxiomC{$\myctx \vdash \mytyb \mycumul \myse{C}$}
3354 \BinaryInfC{$\myctx \vdash \mytya \mycumul \myse{C}$}
3360 \begin{tabular}{ccc}
3361 \AxiomC{$\myjud{\mytmt}{\mytya}$}
3362 \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3363 \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
3366 \AxiomC{$\myctx \vdash \mytya_1 \mydefeq \mytya_2$}
3367 \AxiomC{$\myctx; \myb{x} : \mytya_1 \vdash \mytyb_1 \mycumul \mytyb_2$}
3368 \BinaryInfC{$\myctx (\myb{x} {:} \mytya_1) \myarr \mytyb_1 \mycumul (\myb{x} {:} \mytya_2) \myarr \mytyb_2$}
3372 \caption{Cumulativity rules for base types in \mykant, plus a
3373 `conversion' rule for cumulative types.}
3374 \label{fig:cumulativity}
3377 One way to solve this issue is a \emph{cumulative} hierarchy, where
3378 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$. This way we retain
3379 consistency, while allowing for `large' definitions that work on small
3382 \begin{mydef}[Cumulativity for \mykant' base types]
3383 Figure \ref{fig:cumulativity} gives a formal definition of
3384 \emph{cumulativity} for the base types. Similar measures can be taken
3385 for user defined types, withe the type living in the least upper bound
3386 of the levels where the types contained data live.
3388 For example we might define our disjunction to be
3390 \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
3392 And hope that $\mytyp_{100}$ will be large enough to fit all the types
3393 that we want to use with our disjunction. However, there are two
3394 problems with this. First, clumsiness of having to manually specify the
3395 size of types is still there. More importantly, if we want to use
3396 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
3397 make sure that those allow for types at least as large as
3400 A better option is to employ a mechanised version of what Russell called
3401 \emph{typical ambiguity}: we let the user live under the illusion that
3402 $\mytyp : \mytyp$, but check that the statements about types are
3403 consistent under the hood. $\mykant$\ implements this following the
3404 plan given by \cite{Huet1988}. See also \cite{Harper1991} for a
3405 published reference, although describing a more complex system allowing
3406 for both explicit and explicit hierarchy at the same time.
3408 We define a partial ordering on the levels, with both weak ($\le$) and
3409 strong ($<$) constraints, the laws governing them being the same as the
3410 ones governing $<$ and $\le$ for the natural numbers. Each occurrence
3411 of $\mytyp$ is decorated with a unique reference. We keep a set of
3412 constraints regarding the ordering of each occurrence of $\mytyp$, each
3413 represented by its unique reference. We add new constraints as we type
3414 check, generating new references when needed.
3416 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
3417 denotes the unique reference assigned to that term, we will generate a
3418 new fresh reference and return the type $\mytyp\, r_2$, adding the
3419 constraint $r_1 < r_2$ to the set. When type checking $\myctx \vdash
3420 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
3421 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
3422 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
3425 If at any point the constraint set becomes inconsistent, type checking
3426 fails. Moreover, when comparing two $\mytyp$ terms---during the process
3427 of deciding definitional equality for two terms---we equate their
3428 respective references with two $\le$ constraints. Implementation
3429 details are given in Section \ref{sec:hier-impl}.
3431 Another more flexible but also more verbose alternative is the one
3432 chosen by Agda, where levels can be quantified so that the relationship
3433 between arguments and result in type formers can be explicitly
3436 \myarg\myfun{$\vee$}\myarg : (l_1\, l_2 : \mytyc{Level}) \myarr \mytyp_{l_1} \myarr \mytyp_{l_2} \myarr \mytyp_{l_1 \mylub l_2}
3438 Inference algorithms to automatically derive this kind of relationship
3439 are currently subject of research. We choose a less flexible but more
3440 concise way, since it is easier to implement and better understood.
3442 \subsection{Observational equality, \mykant\ style}
3444 There are two correlated differences between $\mykant$\ and the theory
3445 used to present OTT. The first is that in $\mykant$ we have a type
3446 hierarchy, which lets us, for example, abstract over types. The second
3447 is that we let the user define inductive types and records.
3449 Reconciling propositions for OTT and a hierarchy had already been
3450 investigated by Conor McBride,\footnote{See
3451 \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.} and we
3452 follow some of his suggestions, with some innovation. Most of the dirty
3453 work, as an extension of elaboration, is to handle reduction rules and
3454 coercions for data types---both type constructors and data constructors.
3456 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
3458 Before defining $\myprop$, we define some basic types inside $\mykant$,
3459 as the target for the $\myprop$ decoder.
3460 \begin{mydef}[\mykant' propositional prelude]\ \end{mydef}
3463 \myadt{\mytyc{Empty}}{}{ }{ } \\
3464 \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
3465 \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
3468 \myreco{\mytyc{Unit}}{}{}{ } \\ \ \\
3470 \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
3474 \begin{mydef}[Propositions and decoding]\ \end{mydef}
3478 \begin{array}{r@{\ }c@{\ }l}
3479 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
3480 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
3485 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
3488 \begin{array}{l@{\ }c@{\ }l}
3489 \myprdec{\mybot} & \myred & \myempty \\
3490 \myprdec{\mytop} & \myred & \myunit
3495 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
3496 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \mytyc{Prod} \myappsp \myprdec{\myse{P}} \myappsp \myprdec{\myse{Q}} \\
3497 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
3498 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
3504 We will overload the $\myand$ symbol to define `nested' products, and
3505 $\myproj{n}$ to project elements from them, so that
3508 \mytya \myand \mytyb = \mytya \myand (\mytyb \myand \mytop) \\
3509 \mytya \myand \mytyb \myand \myse{C} = \mytya \myand (\mytyb \myand (\myse{C} \myand \mytop)) \\
3511 \myproj{1} : \myprdec{\mytya \myand \mytyb} \myarr \myprdec{\mytya} \\
3512 \myproj{2} : \myprdec{\mytya \myand \mytyb \myand \myse{C}} \myarr \myprdec{\mytyb} \\
3516 And so on, so that $\myproj{n}$ will work with all products with at
3517 least than $n$ elements. Logically a 0-ary $\myand$ will correspond to
3520 \subsubsection{Some OTT examples}
3522 Before presenting the direction that $\mykant$\ takes, let us consider
3523 two examples of use-defined data types, and the result we would expect
3524 given what we already know about OTT, assuming the same propositional
3529 \item[Product types] Let us consider first the already mentioned
3530 dependent product, using the alternate name $\mysigma$\footnote{For
3531 extra confusion, `dependent products' are often called `dependent
3532 sums' in the literature, referring to the interpretation that
3533 identifies the first element as a `tag' deciding the type of the
3534 second element, which lets us recover sum types (disjuctions), as we
3535 saw in Section \ref{sec:depprod}. Thus, $\mysigma$.} to
3536 avoid confusion with the $\mytyc{Prod}$ in the prelude:
3539 \myreco{\mysigma}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
3542 First type-level equality. The result we want is
3545 \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\
3546 \myind{2} \mytya_1 \myeq \mytya_2 \myand \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \myapp{\mytyb_1}{\myb{x_1}} \myeq \myapp{\mytyb_2}{\myb{x_2}}}
3549 The difference here is that in the original presentation of OTT the
3550 type binders are explicit, while here $\mytyb_1$ and $\mytyb_2$ are
3551 functions returning types. We can do this thanks to the type
3552 hierarchy, and this hints at the fact that heterogeneous equality will
3553 have to allow $\mytyp$ `to the right of the colon'. Indeed,
3554 heterogeneous equalities involving abstractions over types will
3555 provide the solution to simplify the equality above.
3557 If we take, just like we saw previously in OTT
3560 \myjm{\myse{f}_1}{\myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}}{\myse{f}_2}{\myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}} \myred \\
3561 \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3562 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3563 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
3567 Then we can simply have
3570 \mysigma \myappsp \mytya_1 \myappsp \mytyb_1 \myeq \mysigma \myappsp \mytya_2 \myappsp \mytyb_2 \myred \\ \myind{2} \mytya_1 \myeq \mytya_2 \myand \myjm{\mytyb_1}{\mytya_1 \myarr \mytyp}{\mytyb_2}{\mytya_2 \myarr \mytyp}
3573 Which will reduce to precisely what we desire, but with an
3574 heterogeneous equalities relating types instead of values:
3577 \mytya_1 \myeq \mytya_2 \myand \myjm{\mytyb_1}{\mytya_1 \myarr \mytyp}{\mytyb_2}{\mytya_2 \myarr \mytyp} \myred \\
3578 \mytya_1 \myeq \mytya_2 \myand
3579 \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3580 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3581 \myjm{\myapp{\mytyb_1}{\myb{x_1}}}{\mytyp}{\myapp{\mytyb_2}{\myb{x_2}}}{\mytyp}
3585 If we pretend for the moment that those heterogeneous equalities were
3586 type equalities, things run smoothly. For what concerns coercions and
3587 quotation, things stay the same (apart from the fact that we apply to
3588 the second argument instead of substituting). We can recognise
3589 records such as $\mysigma$ as such and employ projections in value
3590 equality and coercions; as to not impede progress if not necessary.
3592 \item[Lists] Now for finite lists, which will give us a taste for data
3596 \myadt{\mylist}{\myappsp (\myb{A} {:} \mytyp)}{ }{\mydc{nil} \mydcsep \mydc{cons} \myappsp \myb{A} \myappsp (\myapp{\mylist}{\myb{A}})}
3599 Type equality is simple---we only need to compare the parameter:
3601 \mylist \myappsp \mytya_1 \myeq \mylist \myappsp \mytya_2 \myred \mytya_1 \myeq \mytya_2
3603 For coercions, we transport based on the constructor, recycling the
3604 proof for the inductive occurrence:
3606 \begin{array}{@{}l@{\ }c@{\ }l}
3607 \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mydc{nil} & \myred & \mydc{nil} \\
3608 \mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp (\mydc{cons} \myappsp \mytmm \myappsp \mytmn) & \myred & \\
3609 \multicolumn{3}{l}{\myind{2} \mydc{cons} \myappsp (\mycoe \myappsp \mytya_1 \myappsp \mytya_2 \myappsp \myse{Q} \myappsp \mytmm) \myappsp (\mycoe \myappsp (\mylist \myappsp \mytya_1) \myappsp (\mylist \myappsp \mytya_2) \myappsp \myse{Q} \myappsp \mytmn)}
3612 Value equality is unsurprising---we match the constructors, and
3613 return bottom for mismatches. However, we also need to equate the
3614 parameter in $\mydc{nil}$:
3616 \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
3617 (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mytya_1 \myeq \mytya_2 \\
3618 (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \\
3619 & \multicolumn{11}{@{}l}{ \myind{2}
3620 \myjm{\mytmm_1}{\mytya_1}{\mytmm_2}{\mytya_2} \myand \myjm{\mytmn_1}{\myapp{\mylist}{\mytya_1}}{\mytmn_2}{\myapp{\mylist}{\mytya_2}}
3622 (& \mydc{nil} & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{cons} \myappsp \mytmm_2 \myappsp \mytmn_2 & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot \\
3623 (& \mydc{cons} \myappsp \mytmm_1 \myappsp \mytmn_1 & : & \myapp{\mylist}{\mytya_1} &) & \myeq & (& \mydc{nil} & : & \myapp{\mylist}{\mytya_2} &) \myred \mybot
3628 \subsubsection{Only one equality}
3630 Given the examples above, a more `flexible' heterogeneous equality must
3631 emerge, since of the fact that in $\mykant$ we re-gain the possibility
3632 of abstracting and in general handling types in a way that was not
3633 possible in the original OTT presentation. Moreover, we found that the
3634 rules for value equality work well if used with user defined type
3635 abstractions---for example in the case of dependent products we recover
3636 the original definition with explicit binders, in a natural manner.
3638 \begin{mydef}[Propositions, coercions, coherence, equalities and
3639 equality reduction for \mykant] See Figure \ref{fig:kant-eq-red}.
3642 \begin{mydef}[Type equality in \mykant]
3643 We define $\mytya \myeq \mytyb$ as an abbreviation for
3644 $\myjm{\mytya}{\mytyp}{\mytyb}{\mytyp}$.
3647 In fact, we can drop a separate notion of type-equality, which will
3648 simply be served by $\myjm{\mytya}{\mytyp}{\mytyb}{\mytyp}$. We shall
3649 still distinguish equalities relating types for hierarchical
3650 purposes. We exploit record to perform $\eta$-expansion. Moreover,
3651 given the nested $\myand$s, values of data types with zero constructors
3652 (such as $\myempty$) and records with zero destructors (such as
3653 $\myunit$) will be automatically always identified as equal. As in the
3654 original OTT, and for the same reasons, we can take $\myfun{coh}$ as
3662 \begin{array}{r@{\ }c@{\ }l}
3663 \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
3664 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3665 \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
3672 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
3675 \AxiomC{$\mychk{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3676 \AxiomC{$\mychk{\mytmt}{\mytya}$}
3677 \BinaryInfC{$\myinf{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
3680 \AxiomC{$\mychk{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
3681 \AxiomC{$\mychk{\mytmt}{\mytya}$}
3682 \BinaryInfC{$\myinf{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
3689 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
3692 \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
3693 \UnaryInfC{$\myjud{\mytop}{\myprop}$}
3695 \UnaryInfC{$\myjud{\mybot}{\myprop}$}
3698 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
3699 \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
3700 \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
3702 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
3711 \phantom{\myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}}} \\
3712 \myjud{\myse{A}}{\mytyp}\hspace{0.8cm}
3713 \myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}
3716 \UnaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
3721 \myjud{\myse{A}}{\mytyp} \hspace{0.8cm} \myjud{\mytmm}{\myse{A}} \\
3722 \myjud{\myse{B}}{\mytyp} \hspace{0.8cm} \myjud{\mytmn}{\myse{B}}
3725 \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
3732 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
3736 \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
3740 \UnaryInfC{$\myctx \vdash \myjm{\myprdec{\myse{P}}}{\mytyp}{\myprdec{\myse{Q}}}{\mytyp} \myred \mytop$}
3748 \begin{array}{@{}r@{\ }l}
3750 \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp} \myred \\
3751 & \myind{2} \mytya_2 \myeq \mytya_1 \myand \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
3752 \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1[\myb{x_1}] \myeq \mytyb_2[\myb{x_2}]
3762 \begin{array}{@{}r@{\ }l}
3764 \myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}} \myred \\
3765 & \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
3766 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
3767 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1[\myb{x_1}]}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2[\myb{x_2}]}
3776 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
3778 \begin{array}{r@{\ }l}
3780 \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp} \myred \\
3781 & \myind{2} \mybigand_{i = 1}^n (\myjm{\mytya_n}{\myhead(\mytele(A_1 \cdots A_{i-1}))}{\mytyb_i}{\myhead(\mytele(B_1 \cdots B_{i-1}))})
3790 \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
3791 \mytyc{D}.\mydc{c} : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \hspace{0.8cm}
3792 \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
3793 \mytele_B = (\mytele;\mytele')\vec{B}
3797 \begin{array}{@{}l@{\ }l}
3798 \myctx \vdash & \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \\
3799 & \myind{2} \mybigand_{i=1}^n(\myjm{\mytmm_i}{\myhead(\mytele_A (\mytya_i \cdots \mytya_{i-1}))}{\mytmn_i}{\myhead(\mytele_B (\mytyb_i \cdots \mytyb_{i-1}))})
3806 \AxiomC{$\mydataty(\mytyc{D}, \myctx)$}
3808 \myctx \vdash \myjm{\mytyc{D}.\mydc{c} \myappsp \vec{\myse{l}}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c'} \myappsp \vec{\myse{r}}}{\mytyc{D} \myappsp \vec{B}} \myred \mybot
3816 \myisreco(\mytyc{D}, \myctx)\hspace{0.8cm}
3817 \mytyc{D}.\myfun{f}_i : \mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i \in \myctx\\
3821 \begin{array}{@{}l@{\ }l}
3822 \myctx \vdash & \myjm{\myse{l}}{\mytyc{D} \myappsp \vec{A}}{\myse{r}}{\mytyc{D} \myappsp \vec{B}} \myred \\ & \myind{2} \mybigand_{i=1}^n(\myjm{\mytyc{D}.\myfun{f}_1 \myappsp \myse{l}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytya};\myse{l})}{\mytyc{D}.\myfun{f}_i \myappsp \myse{r}}{(\mytele; (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}_i)(\vec{\mytyb};\myse{r})})
3829 \UnaryInfC{$\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb} \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
3832 \caption{Propositions and equality reduction in $\mykant$. We assume
3833 the presence of $\mydataty$ and $\myisreco$ as operations on the
3834 context to recognise whether a user defined type is a data type or a
3836 \label{fig:kant-eq-red}
3839 \subsubsection{Coercions}
3841 For coercions the algorithm is messier and not reproduced here for lack
3842 of a decent notation---the details are hairy but uninteresting. To give
3843 an idea of the possible complications, let us conceive a type that
3844 showcases trouble not arising in the previous examples.
3847 \myadt{\mytyc{Max}}{\myappsp (\myb{A} {:} \mynat \myarr \mytyp) \myappsp (\myb{B} {:} (\myb{x} {:} \mynat) \myarr \myb{A} \myappsp \myb{x} \myarr \mytyp) \myappsp (\myb{k} {:} \mynat)}{ \\ \myind{2}}{
3848 \mydc{max} \myappsp (\myb{A} \myappsp \myb{k}) \myappsp (\myb{x} {:} \mynat) \myappsp (\myb{a} {:} \myb{A} \myappsp \myb{x}) \myappsp (\myb{B} \myappsp \myb{x} \myappsp \myb{a})
3852 For type equalities we will have
3854 \begin{array}{@{}l@{\ }l}
3855 \myjm{\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1}{\mytyp}{\mytyc{Max} \myappsp \mytya_2 \myappsp \myappsp \mytyb_2 \myappsp \myse{k}_2}{\mytyp} & \myred \\[0.2cm]
3857 \myjm{\mytya_1}{\mynat \myarr \mytyp}{\mytya_2}{\mynat \myarr \mytyp} \myand \\
3858 \myjm{\mytyb_1}{(\myb{x} {:} \mynat) \myarr \mytya_1 \myappsp \myb{x} \myarr \mytyp}{\mytyb_2}{(\myb{x} {:} \mynat) \myarr \mytya_2 \myappsp \myb{x} \myarr \mytyp} \\
3859 \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3860 \end{array} & \myred \\[0.7cm]
3862 (\mynat \myeq \mynat \myand (\myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \myapp{\mytya_1}{\myb{x_1}} \myeq \myapp{\mytya_2}{\myb{x_2}}})) \myand \\
3863 (\mynat \myeq \mynat \myand \left(
3865 \myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \\ \myjm{\mytyb_1 \myappsp \myb{x_1}}{\mytya_1 \myappsp \myb{x_1} \myarr \mytyp}{\mytyb_2 \myappsp \myb{x_2}}{\mytya_2 \myappsp \myb{x_2} \myarr \mytyp}}
3868 \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3869 \end{array} & \myred \\[0.9cm]
3871 (\mytop \myand (\myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \myapp{\mytya_1}{\myb{x_1}} \myeq \myapp{\mytya_2}{\myb{x_2}}})) \myand \\
3872 (\mytop \myand \left(
3874 \myprfora{\myb{x_1}\, \myb{x_2}}{\mynat}{\myjm{\myb{x_1}}{\mynat}{\myb{x_2}}{\mynat} \myimpl \\
3875 \myprfora{\myb{y_1}}{\mytya_1 \myappsp \myb{x_1}}{\myprfora{\myb{y_2}}{\mytya_2 \myappsp \myb{x_2}}{\myjm{\myb{y_1}}{\mytya_1 \myappsp \myb{x_1}}{\myb{y_2}}{\mytya_2 \myappsp \myb{x_2}} \myimpl \\
3876 \mytyb_1 \myappsp \myb{x_1} \myappsp \myb{y_1} \myeq \mytyb_2 \myappsp \myb{x_2} \myappsp \myb{y_2}}}}
3879 \myjm{\myse{k}_1}{\mynat}{\myse{k}_2}{\mynat}
3883 The result, while looking complicated, is actually saying something
3884 simple---given equal inputs, the parameters for $\mytyc{Max}$ will
3885 return equal types. Moreover, we have evidence that the two $\myb{k}$
3886 parameters are equal. When coercing, we need to mechanically generate
3887 one proof of equality for each argument, and then coerce:
3890 \mycoee{(\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1)}{(\mytyc{Max} \myappsp \mytya_2 \myappsp \mytyb_2 \myappsp \myse{k}_2)}{\myse{Q}}{(\mydc{max} \myappsp \myse{ak}_1 \myappsp \myse{n}_1 \myappsp \myse{a}_1 \myappsp \myse{b}_1)} \myred \\
3892 \begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
3893 \mysyn{let} & \myb{Q_{Ak}} & \mapsto & \myhole{?} : \myprdec{\mytya_1 \myappsp \myse{k}_1 \myeq \mytya_2 \myappsp \myse{k}_2} \\
3894 & \myb{ak_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{k}_1)}{(\mytya_2 \myappsp \myse{k}_2)}{\myb{Q_{Ak}}}{\myse{ak_1}} : \mytya_1 \myappsp \myse{k}_2 \\
3895 & \myb{Q_{\mathbb{N}}} & \mapsto & \myhole{?} : \myprdec{\mynat \myeq \mynat} \\
3896 & \myb{n_2} & \mapsto & \mycoee{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}} : \mynat \\
3897 & \myb{Q_A} & \mapsto & \myhole{?} : \myprdec{\mytya_1 \myappsp \myse{n_1} \myeq \mytya_2 \myappsp \myb{n_2}} \\
3898 & \myb{a_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myb{n_2})}{\myb{Q_A}} : \mytya_2 \myappsp \myb{n_2} \\
3899 & \myb{Q_B} & \mapsto & \myhole{?} : \myprdec{\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a}_1 \myeq \mytyb_1 \myappsp \myb{n_2} \myappsp \myb{a_2}} \\
3900 & \myb{b_2} & \mapsto & \mycoee{(\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a_1})}{(\mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2})}{\myb{Q_B}} : \mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2} \\
3901 \mysyn{in} & \multicolumn{3}{@{}l}{\mydc{max} \myappsp \myb{ak_2} \myappsp \myb{n_2} \myappsp \myb{a_2} \myappsp \myb{b_2}}
3905 For equalities regarding types that are external to the data type we can
3906 derive a proof by reflexivity by invoking $\mydc{refl}$ as defined in
3907 Section \ref{sec:lazy}, and the instantiate arguments if we need too.
3908 In this case, for $\mynat$, we do not have any arguments. For
3909 equalities concerning arguments of the type constructor or already
3910 coerced arguments of the type constructor we have to refer to the right
3911 proof and use $\mycoh$erence when due, which is where the technical
3915 \mycoee{(\mytyc{Max} \myappsp \mytya_1 \myappsp \mytyb_1 \myappsp \myse{k}_1)}{(\mytyc{Max} \myappsp \mytya_2 \myappsp \mytyb_2 \myappsp \myse{k}_2)}{\myse{Q}}{(\mydc{max} \myappsp \myse{ak}_1 \myappsp \myse{n}_1 \myappsp \myse{a}_1 \myappsp \myse{b}_1)} \myred \\
3917 \begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
3918 \mysyn{let} & \myb{Q_{Ak}} & \mapsto & (\myproj{2} \myappsp (\myproj{1} \myappsp \myse{Q})) \myappsp \myse{k_1} \myappsp \myse{k_2} \myappsp (\myproj{3} \myappsp \myse{Q}) : \myprdec{\mytya_1 \myappsp \myse{k}_1 \myeq \mytya_2 \myappsp \myse{k}_2} \\
3919 & \myb{ak_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{k}_1)}{(\mytya_2 \myappsp \myse{k}_2)}{\myb{Q_{Ak}}}{\myse{ak_1}} : \mytya_1 \myappsp \myse{k}_2 \\
3920 & \myb{Q_{\mathbb{N}}} & \mapsto & \mydc{refl} \myappsp \mynat : \myprdec{\mynat \myeq \mynat} \\
3921 & \myb{n_2} & \mapsto & \mycoee{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}} : \mynat \\
3922 & \myb{Q_A} & \mapsto & (\myproj{2} \myappsp (\myproj{1} \myappsp \myse{Q})) \myappsp \myse{n_1} \myappsp \myb{n_2} \myappsp (\mycohh{\mynat}{\mynat}{\myb{Q_{\mathbb{N}}}}{\myse{n_1}}) : \myprdec{\mytya_1 \myappsp \myse{n_1} \myeq \mytya_2 \myappsp \myb{n_2}} \\
3923 & \myb{a_2} & \mapsto & \mycoee{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myb{n_2})}{\myb{Q_A}} : \mytya_2 \myappsp \myb{n_2} \\
3924 & \myb{Q_B} & \mapsto & (\myproj{2} \myappsp (\myproj{2} \myappsp \myse{Q})) \myappsp \myse{n_1} \myappsp \myb{n_2} \myappsp \myb{Q_{\mathbb{N}}} \myappsp \myse{a_1} \myappsp \myb{a_2} \myappsp (\mycohh{(\mytya_1 \myappsp \myse{n_1})}{(\mytya_2 \myappsp \myse{n_2})}{\myb{Q_A}}{\myse{a_1}}) : \myprdec{\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a}_1 \myeq \mytyb_1 \myappsp \myb{n_2} \myappsp \myb{a_2}} \\
3925 & \myb{b_2} & \mapsto & \mycoee{(\mytyb_1 \myappsp \myse{n_1} \myappsp \myse{a_1})}{(\mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2})}{\myb{Q_B}} : \mytyb_2 \myappsp \myb{n_2} \myappsp \myb{a_2} \\
3926 \mysyn{in} & \multicolumn{3}{@{}l}{\mydc{max} \myappsp \myb{ak_2} \myappsp \myb{n_2} \myappsp \myb{a_2} \myappsp \myb{b_2}}
3931 \subsubsection{$\myprop$ and the hierarchy}
3933 We shall have, at each universe level, not only a $\mytyp_l$ but also a
3934 $\myprop_l$. Where will propositions placed in the type hierarchy? The
3935 main indicator is the decoding operator, since it converts into things
3936 that already live in the hierarchy. For example, if we have
3938 \myprdec{\mynat \myarr \mybool \myeq \mynat \myarr \mybool} \myred
3939 \mytop \myand ((\myb{x}\, \myb{y} : \mynat) \myarr \mytop \myarr \mytop)
3941 we will better make sure that the `to be decoded' is at level compatible
3942 (read: larger) with its reduction. In the example above, we will have
3943 that proposition to be at least as large as the type of $\mynat$, since
3944 the reduced proof will abstract over it. Pretending that we had
3945 explicit, non cumulative levels, it would be tempting to have
3948 \AxiomC{$\myjud{\myse{Q}}{\myprop_l}$}
3949 \UnaryInfC{$\myjud{\myprdec{\myse{Q}}}{\mytyp_l}$}
3952 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3953 \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3954 \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3958 $\mybot$ and $\mytop$ living at any level, $\myand$ and $\forall$
3959 following rules similar to the ones for $\myprod$ and $\myarr$ in
3960 Section \ref{sec:itt}. However, we need to be careful with value
3961 equality since for example we have that
3963 \myprdec{\myjm{\myse{f}_1}{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\myse{f}_2}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}}
3965 \myfora{\myb{x_1}}{\mytya_1}{\myfora{\myb{x_2}}{\mytya_2}{\cdots}}
3967 where the proposition decodes into something of at least type $\mytyp_l$, where
3968 $\mytya_l : \mytyp_l$ and $\mytyb_l : \mytyp_l$. We can resolve this
3969 tension by making all equalities larger:
3971 \AxiomC{$\myjud{\mytmm}{\mytya}$}
3972 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3973 \AxiomC{$\myjud{\mytmn}{\mytyb}$}
3974 \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3975 \QuaternaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
3977 This is disappointing, since type equalities will be needlessly large:
3978 $\myprdec{\myjm{\mytya}{\mytyp_l}{\mytyb}{\mytyp_l}} : \mytyp_{l + 1}$.
3980 However, considering that our theory is cumulative, we can do better.
3981 Assuming rules for $\myprop$ cumulativity similar to the ones for
3982 $\mytyp$, we will have (with the conversion rule reproduced as a
3986 \AxiomC{$\myctx \vdash \mytya \mycumul \mytyb$}
3987 \AxiomC{$\myjud{\mytmt}{\mytya}$}
3988 \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
3991 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
3992 \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
3993 \BinaryInfC{$\myjud{\myjm{\mytya}{\mytyp_{l}}{\mytyb}{\mytyp_{l}}}{\myprop_l}$}
3999 \AxiomC{$\myjud{\mytmm}{\mytya}$}
4000 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
4001 \AxiomC{$\myjud{\mytmn}{\mytyb}$}
4002 \AxiomC{$\myjud{\mytyb}{\mytyp_l}$}
4003 \AxiomC{$\mytya$ and $\mytyb$ are not $\mytyp_{l'}$}
4004 \QuinaryInfC{$\myjud{\myjm{\mytmm}{\mytya}{\mytmm}{\mytya}}{\myprop_l}$}
4008 That is, we are small when we can (type equalities) and large otherwise.
4009 This would not work in a non-cumulative theory because subject reduction
4010 would not hold. Consider for instance
4012 \myjm{\mynat}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}{\mybool}{\myITE{\mytrue}{\mytyp_0}{\mytyp_0}}
4016 \[\myjm{\mynat}{\mytyp_0}{\mybool}{\mytyp_0} : \myprop_0 \]
4017 We need members of $\myprop_0$ to be members of $\myprop_1$ too, which
4018 will be the case with cumulativity. This buys us a cheap type level
4019 equality without having to replicate functionality with a dedicated
4022 \subsubsection{Quotation and definitional equality}
4023 \label{sec:kant-irr}
4025 Now we can give an account of definitional equality, by explaining how
4026 to perform quotation (as defined in Section \ref{sec:eta-expand})
4027 towards the goal described in Section \ref{sec:ott-quot}.
4031 \item Perform $\eta$-expansion on functions and records.
4033 \item As a consequence of the previous point, identify all records with
4034 no projections as equal, since they will have only one element.
4036 \item Identify all members of types with no constructors (and thus no
4039 \item Identify all equivalent proofs as equal---with `equivalent proof'
4040 we mean those proving the same propositions.
4042 \item Advance coercions working across definitionally equal types.
4044 Towards these goals and following the intuition between bidirectional
4045 type checking we define two mutually recursive functions, one quoting
4046 canonical terms against their types (since we need the type to type check
4047 canonical terms), one quoting neutral terms while recovering their
4049 \begin{mydef}[Quotation for \mykant]
4050 The full procedure for quotation is shown in Figure
4051 \ref{fig:kant-quot}.
4053 We $\boxed{\text{box}}$ the neutral proofs and
4054 neutral members of empty types, following the notation in
4055 \cite{Altenkirch2007}, and we make use of $\mydefeq_{\mybox}$ which
4056 compares terms syntactically up to $\alpha$-renaming, but also up to
4057 equivalent proofs: we consider all boxed content as equal.
4059 Our quotation will work on normalised terms, so that all defined values
4060 will have been replaced. Moreover, we match on data type eliminators
4061 and all their arguments, so that $\mynat.\myfun{elim} \myappsp \mytmm
4062 \myappsp \myse{P} \myappsp \vec{\mytmn}$ will stand for
4063 $\mynat.\myfun{elim}$ applied to the scrutinised $\mynat$, the
4064 predicate, and the two cases. This measure can be easily implemented by
4065 checking the head of applications and `consuming' the needed terms.
4066 Thus, we gain proof irrelevance, and not only for a more useful
4067 definitional equality, but also for example to eliminate all
4068 propositional content when compiling.
4071 \mydesc{canonical quotation:}{\mycanquot(\myctx, \mytmsyn : \mytmsyn) \mymetagoes \mytmsyn}{
4074 \begin{array}{@{}l@{}l}
4075 \mycanquot(\myctx,\ \mytmt : \mytyc{D} \myappsp \vec{A} &) \mymetaguard \mymeta{empty}(\myctx, \mytyc{D}) \mymetagoes \boxed{\mytmt} \\
4076 \mycanquot(\myctx,\ \mytmt : \mytyc{D} \myappsp \vec{A} &) \mymetaguard \mymeta{record}(\myctx, \mytyc{D}) \mymetagoes
4077 \mytyc{D}.\mydc{constr} \myappsp \cdots \myappsp \mycanquot(\myctx, \mytyc{D}.\myfun{f}_n : (\myctx(\mytyc{D}.\myfun{f}_n))(\vec{A};\mytmt)) \\
4078 \mycanquot(\myctx,\ \mytyc{D}.\mydc{c} \myappsp \vec{t} : \mytyc{D} \myappsp \vec{A} &) \mymetagoes \cdots \\
4079 \mycanquot(\myctx,\ \myse{f} : \myfora{\myb{x}}{\mytya}{\mytyb} &) \mymetagoes \myabs{\myb{x}}{\mycanquot(\myctx; \myb{x} : \mytya, \myapp{\myse{f}}{\myb{x}} : \mytyb)} \\
4080 \mycanquot(\myctx,\ \myse{p} : \myprdec{\myse{P}} &) \mymetagoes \boxed{\myse{p}}
4082 \mycanquot(\myctx,\ \mytmt : \mytya &) \mymetagoes \mytmt'\ \text{\textbf{where}}\ \mytmt' : \myarg = \myneuquot(\myctx, \mytmt)
4089 \mydesc{neutral quotation:}{\myneuquot(\myctx, \mytmsyn) \mymetagoes \mytmsyn : \mytmsyn}{
4092 \begin{array}{@{}l@{}l}
4093 \myneuquot(\myctx,\ \myb{x} &) \mymetagoes \myb{x} : \myctx(\myb{x}) \\
4094 \myneuquot(\myctx,\ \mytyp &) \mymetagoes \mytyp : \mytyp \\
4095 \myneuquot(\myctx,\ \myfora{\myb{x}}{\mytya}{\mytyb} & ) \mymetagoes
4096 \myfora{\myb{x}}{\myneuquot(\myctx, \mytya)}{\myneuquot(\myctx; \myb{x} : \mytya, \mytyb)} : \mytyp \\
4097 \myneuquot(\myctx,\ \mytyc{D} \myappsp \vec{A} &) \mymetagoes \mytyc{D} \myappsp \cdots \mycanquot(\myctx, \mymeta{head}((\myctx(\mytyc{D}))(\mytya_1 \cdots \mytya_{n-1}))) : \mytyp \\
4098 \myneuquot(\myctx,\ \myprdec{\myjm{\mytmm}{\mytya}{\mytmn}{\mytyb}} &) \mymetagoes \\
4099 \multicolumn{2}{l}{\myind{2}\myprdec{\myjm{\mycanquot(\myctx, \mytmm : \mytya)}{\mytya'}{\mycanquot(\myctx, \mytmn : \mytyb)}{\mytyb'}} : \mytyp} \\
4100 \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytya' : \myarg = \myneuquot(\myctx, \mytya)} \\
4101 \multicolumn{2}{@{}l}{\myind{2}\phantom{\text{\textbf{where}}}\ \mytyb' : \myarg = \myneuquot(\myctx, \mytyb)} \\
4102 \myneuquot(\myctx,\ \mytyc{D}.\myfun{f} \myappsp \mytmt &) \mymetaguard \mymeta{record}(\myctx, \mytyc{D}) \mymetagoes \mytyc{D}.\myfun{f} \myappsp \mytmt' : (\myctx(\mytyc{D}.\myfun{f}))(\vec{A};\mytmt) \\
4103 \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytmt' : \mytyc{D} \myappsp \vec{A} = \myneuquot(\myctx, \mytmt)} \\
4104 \myneuquot(\myctx,\ \mytyc{D}.\myfun{elim} \myappsp \mytmt \myappsp \myse{P} &) \mymetaguard \mymeta{empty}(\myctx, \mytyc{D}) \mymetagoes \mytyc{D}.\myfun{elim} \myappsp \boxed{\mytmt} \myappsp \myneuquot(\myctx, \myse{P}) : \myse{P} \myappsp \mytmt \\
4105 \myneuquot(\myctx,\ \mytyc{D}.\myfun{elim} \myappsp \mytmm \myappsp \myse{P} \myappsp \vec{\mytmn} &) \mymetagoes \mytyc{D}.\myfun{elim} \myappsp \mytmm' \myappsp \myneuquot(\myctx, \myse{P}) \cdots : \myse{P} \myappsp \mytmm\\
4106 \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \mytmm' : \mytyc{D} \myappsp \vec{A} = \myneuquot(\myctx, \mytmm)} \\
4107 \myneuquot(\myctx,\ \myapp{\myse{f}}{\mytmt} &) \mymetagoes \myapp{\myse{f'}}{\mycanquot(\myctx, \mytmt : \mytya)} : \mysub{\mytyb}{\myb{x}}{\mytmt} \\
4108 \multicolumn{2}{@{}l}{\myind{2}\text{\textbf{where}}\ \myse{f'} : \myfora{\myb{x}}{\mytya}{\mytyb} = \myneuquot(\myctx, \myse{f})} \\
4109 \myneuquot(\myctx,\ \mycoee{\mytya}{\mytyb}{\myse{Q}}{\mytmt} &) \mymetaguard \myneuquot(\myctx, \mytya) \mydefeq_{\mybox} \myneuquot(\myctx, \mytyb) \mymetagoes \myneuquot(\myctx, \mytmt) \\
4110 \myneuquot(\myctx,\ \mycoee{\mytya}{\mytyb}{\myse{Q}}{\mytmt} &) \mymetagoes
4111 \mycoee{\myneuquot(\myctx, \mytya)}{\myneuquot(\myctx, \mytyb)}{\boxed{\myse{Q}}}{\myneuquot(\myctx, \mytmt)}
4115 \caption{Quotation in \mykant. Along the already used
4116 $\mymeta{record}$ meta-operation on the context we make use of
4117 $\mymeta{empty}$, which checks if a certain type constructor has
4118 zero data constructor. The `data constructor' cases for non-record,
4119 non-empty, data types are omitted for brevity.}
4120 \label{fig:kant-quot}
4123 \subsubsection{Why $\myprop$?}
4125 It is worth to ask if $\myprop$ is needed at all. It is perfectly
4126 possible to have the type checker identify propositional types
4127 automatically, and in fact in some sense we already do during equality
4128 reduction and quotation. However, this has the considerable
4129 disadvantage that we can never identify abstracted
4130 variables\footnote{And in general neutral terms, although we currently
4131 do not have neutral propositions apart from equalities on neutral
4132 terms.} of type $\mytyp$ as $\myprop$, thus forbidding the user to
4133 talk about $\myprop$ explicitly.
4135 This is a considerable impediment, for example when implementing
4136 \emph{quotient types}. With quotients, we let the user specify an
4137 equivalence class over a certain type, and then exploit this in various
4138 way---crucially, we need to be sure that the equivalence given is
4139 propositional, a fact which prevented the use of quotients in dependent
4140 type theories \citep{Jacobs1994}.
4142 \section{\mykant : the practice}
4143 \label{sec:kant-practice}
4145 \epigraph{\emph{It's alive!}}{Henry Frankenstein}
4147 The codebase consists of around 2500 lines of Haskell,\footnote{The full
4148 source code is available under the GPL3 license at
4149 \url{https://github.com/bitonic/kant}. `Kant' was a previous
4150 incarnation of the software, and the name remained.} as reported by
4151 the \texttt{cloc} utility.
4153 We implement the type theory as described in Section
4154 \ref{sec:kant-theory}. The author learnt the hard way the
4155 implementation challenges for such a project, and ran out of time while
4156 implementing observational equality. While the constructs and typing
4157 rules are present, the machinery to make it happen (equality reduction,
4158 coercions, quotation, etc.) is not present yet.
4160 This considered, everything else presented in Section
4161 \ref{sec:kant-theory} is implemented and working well---and in fact all
4162 the examples presented in this thesis, apart from the ones that are
4163 equality related, have been encoded in \mykant\ in the Appendix.
4164 Moreover, given the detailed plan in the previous section, finishing off
4165 should not prove too much work.
4167 The interaction with the user takes place in a loop living in and
4168 updating a context of \mykant\ declarations, which presents itself as in
4169 Figure \ref{fig:kant-web}. Files with lists of declarations can also be
4170 loaded. The REPL is a available both as a command-line application and in
4171 a web interface, which is available at \url{bertus.mazzo.li}.
4173 A REPL cycle starts with the user inputting a \mykant\
4174 declaration or another REPL command, which then goes through various
4175 stages that can end up in a context update, or in failures of various
4176 kind. The process is described diagrammatically in figure
4177 \ref{fig:kant-process}.
4180 {\small\begin{Verbatim}[frame=leftline,xleftmargin=3cm]
4182 Version 0.0, made in London, year 2013.
4184 <decl> Declare value/data type/record
4187 :p <term> Pretty print
4189 :r <file> Reload file (erases previous environment)
4190 :i <name> Info about an identifier
4192 >>> :l data/samples/good/common.ka
4194 >>> :e plus three two
4195 suc (suc (suc (suc (suc zero))))
4196 >>> :t plus three two
4201 \caption{A sample run of the \mykant\ prompt.}
4202 \label{fig:kant-web}
4208 \item[Parse] In this phase the text input gets converted to a sugared
4209 version of the core language. For example, we accept multiple
4210 arguments in arrow types and abstractions, and we represent variables
4211 with names, while as we will see in Section \ref{sec:term-repr} the
4212 final term types uses a \emph{nameless} representation.
4214 \item[Desugar] The sugared declaration is converted to a core term.
4215 Most notably we go from names to nameless.
4217 \item[ConDestr] Short for `Constructors/Destructors', converts
4218 applications of data destructors and constructors to a special form,
4219 to perform bidirectional type checking.
4221 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
4222 which is necessary to implement the type hierarchy check.
4224 \item[Elaborate/Typecheck/Evaluate] \textbf{Elaboration} converts the
4225 declaration to some context items, which might be a value declaration
4226 (type and body) or a data type declaration (constructors and
4227 destructors). This phase works in tandem with \textbf{Type checking},
4228 which in turns needs to \textbf{Evaluate} terms.
4230 \item[Distill] and report the result. `Distilling' refers to the
4231 process of converting a core term back to a sugared version that we
4232 can show to the user. This can be necessary both to display errors
4233 including terms or to display result of evaluations or type checking
4234 that the user has requested. Among the other things in this stage we
4235 go from nameless back to names by recycling the names that the user
4236 used originally, as to fabricate a term which is as close as possible
4237 to what it originated from.
4239 \item[Pretty print] Format the terms in a nice way, and display them to
4246 \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
4247 corners, minimum height=2.5em, node distance=0.7cm]
4249 \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
4250 centered, inner sep=0pt, node distance=0.7cm]
4252 \tikzstyle{line} = [draw, -latex']
4254 \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
4255 centered, node distance=1.5cm]
4258 \begin{tikzpicture}[auto]
4259 \node [cloud] (user) {User};
4260 \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
4261 \node [block, below=of parse] (desugar) {Desugar};
4262 \node [block, below=of desugar] (condestr) {ConDestr};
4263 \node [block, below=of condestr] (reference) {Reference};
4264 \node [block, below=of reference] (elaborate) {Elaborate};
4265 \node [block, left=of elaborate] (tycheck) {Typecheck};
4266 \node [block, left=of tycheck] (evaluate) {Evaluate};
4267 \node [decision, right=of elaborate] (error) {Error?};
4268 \node [block, right=of parse] (pretty) {Pretty print};
4269 \node [block, below=of pretty] (distill) {Distill};
4270 \node [block, below=of distill] (update) {Update context};
4272 \path [line] (user) -- (parse);
4273 \path [line] (parse) -- (desugar);
4274 \path [line] (desugar) -- (condestr);
4275 \path [line] (condestr) -- (reference);
4276 \path [line] (reference) -- (elaborate);
4277 \path [line] (elaborate) edge[bend right] (tycheck);
4278 \path [line] (tycheck) edge[bend right] (elaborate);
4279 \path [line] (elaborate) -- (error);
4280 \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
4281 \path [line] (error) -- node [near start] {no} (update);
4282 \path [line] (update) -- (distill);
4283 \path [line] (pretty) -- (user);
4284 \path [line] (distill) -- (pretty);
4285 \path [line] (tycheck) edge[bend right] (evaluate);
4286 \path [line] (evaluate) edge[bend right] (tycheck);
4289 \caption{High level overview of the life of a \mykant\ prompt cycle.}
4290 \label{fig:kant-process}
4293 Here we will review only a sampling of the more interesting
4294 implementation challenges present when implementing an interactive
4299 The syntax of \mykant\ is presented in Figure \ref{fig:syntax}.
4300 Examples showing the usage of most of the constructs---excluding the
4301 OTT-related ones---are present in Appendices \ref{app:kant-itt},
4302 \ref{app:kant-examples}, and \ref{app:hurkens}; plus a tutorial in
4303 Section \ref{sec:type-holes}. The syntax has grown organically with the
4304 needs of the language, and thus is not very sophisticated. The grammar
4305 is specified in and processed by the \texttt{happy} parser generator for
4306 Haskell.\footnote{Available at \url{http://www.haskell.org/happy}.}
4307 Tokenisation is performed by a simple hand written lexer.
4312 \begin{array}{@{\ \ }l@{\ }c@{\ }l}
4313 \multicolumn{3}{@{}l}{\text{A name, in regexp notation.}} \\
4314 \mysee{name} & ::= & \texttt{[a-zA-Z] [a-zA-Z0-9'\_-]*} \\
4315 \multicolumn{3}{@{}l}{\text{A binder might or might not (\texttt{\_}) bind a name.}} \\
4316 \mysee{binder} & ::= & \mytermi{\_} \mysynsep \mysee{name} \\
4317 \multicolumn{3}{@{}l}{\text{A series of typed bindings.}} \\
4318 \mysee{telescope}\, \ \ \ & ::= & (\mytermi{[}\ \mysee{binder}\ \mytermi{:}\ \mysee{term}\ \mytermi{]}){*} \\
4319 \multicolumn{3}{@{}l}{\text{Terms, including propositions.}} \\
4320 \multicolumn{3}{@{}l}{
4321 \begin{array}{@{\ \ }l@{\ }c@{\ }l@{\ \ \ \ \ }l}
4322 \mysee{term} & ::= & \mysee{name} & \text{A variable.} \\
4323 & | & \mytermi{*} & \text{\mytyc{Type}.} \\
4324 & | & \mytermi{\{|}\ \mysee{term}{*}\ \mytermi{|\}} & \text{Type holes.} \\
4325 & | & \mytermi{Prop} & \text{\mytyc{Prop}.} \\
4326 & | & \mytermi{Top} \mysynsep \mytermi{Bot} & \text{$\mytop$ and $\mybot$.} \\
4327 & | & \mysee{term}\ \mytermi{/\textbackslash}\ \mysee{term} & \text{Conjuctions.} \\
4328 & | & \mytermi{[|}\ \mysee{term}\ \mytermi{|]} & \text{Proposition decoding.} \\
4329 & | & \mytermi{coe}\ \mysee{term}\ \mysee{term}\ \mysee{term}\ \mysee{term} & \text{Coercion.} \\
4330 & | & \mytermi{coh}\ \mysee{term}\ \mysee{term}\ \mysee{term}\ \mysee{term} & \text{Coherence.} \\
4331 & | & \mytermi{(}\ \mysee{term}\ \mytermi{:}\ \mysee{term}\ \mytermi{)}\ \mytermi{=}\ \mytermi{(}\ \mysee{term}\ \mytermi{:}\ \mysee{term}\ \mytermi{)} & \text{Heterogeneous equality.} \\
4332 & | & \mytermi{(}\ \mysee{compound}\ \mytermi{)} & \text{Parenthesised term.} \\
4333 \mysee{compound} & ::= & \mytermi{\textbackslash}\ \mysee{binder}{*}\ \mytermi{=>}\ \mysee{term} & \text{Untyped abstraction.} \\
4334 & | & \mytermi{\textbackslash}\ \mysee{telescope}\ \mytermi{:}\ \mysee{term}\ \mytermi{=>}\ \mysee{term} & \text{Typed abstraction.} \\
4335 & | & \mytermi{forall}\ \mysee{telescope}\ \mysee{term} & \text{Universal quantification.} \\
4336 & | & \mysee{arr} \\
4337 \mysee{arr} & ::= & \mysee{telescope}\ \mytermi{->}\ \mysee{arr} & \text{Dependent function.} \\
4338 & | & \mysee{term}\ \mytermi{->}\ \mysee{arr} & \text{Non-dependent function.} \\
4339 & | & \mysee{term}{+} & \text {Application.}
4342 \multicolumn{3}{@{}l}{\text{Typed names.}} \\
4343 \mysee{typed} & ::= & \mysee{name}\ \mytermi{:}\ \mysee{term} \\
4344 \multicolumn{3}{@{}l}{\text{Declarations.}} \\
4345 \mysee{decl}& ::= & \mysee{value} \mysynsep \mysee{abstract} \mysynsep \mysee{data} \mysynsep \mysee{record} \\
4346 \multicolumn{3}{@{}l}{\text{Defined values. The telescope specifies named arguments.}} \\
4347 \mysee{value} & ::= & \mysee{name}\ \mysee{telescope}\ \mytermi{:}\ \mysee{term}\ \mytermi{=>}\ \mysee{term} \\
4348 \multicolumn{3}{@{}l}{\text{Abstracted variables.}} \\
4349 \mysee{abstract} & ::= & \mytermi{postulate}\ \mysee{typed} \\
4350 \multicolumn{3}{@{}l}{\text{Data types, and their constructors.}} \\
4351 \mysee{data} & ::= & \mytermi{data}\ \mysee{name}\ \mysee{telescope}\ \mytermi{->}\ \mytermi{*}\ \mytermi{=>}\ \mytermi{\{}\ \mysee{constrs}\ \mytermi{\}} \\
4352 \mysee{constrs} & ::= & \mysee{typed} \\
4353 & | & \mysee{typed}\ \mytermi{|}\ \mysee{constrs} \\
4354 \multicolumn{3}{@{}l}{\text{Records, and their projections. The $\mysee{name}$ before the projections is the constructor name.}} \\
4355 \mysee{record} & ::= & \mytermi{record}\ \mysee{name}\ \mysee{telescope}\ \mytermi{->}\ \mytermi{*}\ \mytermi{=>}\ \mysee{name}\ \mytermi{\{}\ \mysee{projs}\ \mytermi{\}} \\
4356 \mysee{projs} & ::= & \mysee{typed} \\
4357 & | & \mysee{typed}\ \mytermi{,}\ \mysee{projs}
4361 \caption{\mykant' syntax. The non-terminals are marked with
4362 $\langle\text{angle brackets}\rangle$ for greater clarity. The
4363 syntax in the implementation is actually more liberal, for example
4364 giving the possibility of using arrow types directly in
4365 constructor/projection declarations.\\
4366 Additionally, we give the user the possibility of using Unicode
4367 characters instead of their ASCII counterparts, e.g. \texttt{→} in
4368 place of \texttt{->}, \texttt{λ} in place of
4369 \texttt{\textbackslash}, etc.}
4373 \subsection{Term representation}
4374 \label{sec:term-repr}
4376 \subsubsection{Naming and substituting}
4378 Perhaps surprisingly, one of the most difficult challenges in
4379 implementing a theory of the kind presented is choosing a good data type
4380 for terms, and specifically handling substitutions in a sane way.
4382 There are two broad schools of thought when it comes to naming
4383 variables, and thus substituting:
4385 \item[Nameful] Bound variables are represented by some enumerable data
4386 type, just as we have described up to now, starting from Section
4387 \ref{sec:untyped}. The problem is that avoiding name capturing is a
4388 nightmare, both in the sense that it is not performant---given that we
4389 need to rename rename substitute each time we `enter' a binder---but
4390 most importantly given the fact that in even slightly more complicated
4391 systems it is very hard to get right, even for experts.
4393 One of the sore spots of explicit names is comparing terms up to
4394 $\alpha$-renaming, which again generates a huge amounts of
4395 substitutions and requires special care.
4397 \item[Nameless] We can capture the relationship between variables and
4398 their binders, by getting rid of names altogether, and representing
4399 bound variables with an index referring to the `binding' structure, a
4400 notion introduced by \cite{de1972lambda}. Usually $0$ represents the
4401 variable bound by the innermost binding structure, $1$ the
4402 second-innermost, and so on. For instance with simple abstractions we
4406 \mymacol{red}{\lambda}\, (\mymacol{blue}{\lambda}\, \mymacol{blue}{0}\, (\mymacol{AgdaInductiveConstructor}{\lambda\, 0}))\, (\mymacol{AgdaFunction}{\lambda}\, \mymacol{red}{1}\, \mymacol{AgdaFunction}{0}) : ((\mytya \myarr \mytya) \myarr \mytyb) \myarr \mytyb\text{, which corresponds to} \\
4407 \myabs{\myb{f}}{(\myabs{\myb{g}}{\myapp{\myb{g}}{(\myabs{\myb{x}}{\myb{x}})}}) \myappsp (\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}})} : ((\mytya \myarr \mytya) \myarr \mytyb) \myarr \mytyb
4411 While this technique is obviously terrible in terms of human
4412 usability,\footnote{With some people going as far as defining it akin
4413 to an inverse Turing test.} it is much more convenient as an
4414 internal representation to deal with terms mechanically---at least in
4415 simple cases. $\alpha$-renaming ceases to be an issue, and
4416 term comparison is purely syntactical.
4418 Nonetheless, more complex constructs such as pattern matching put
4419 some strain on the indices and many systems end up using explicit
4424 In the past decade or so advancements in the Haskell's type system and
4425 in general the spread new programming practices have made the nameless
4426 option much more amenable. \mykant\ thus takes the nameless path
4427 through the use of Edward Kmett's excellent \texttt{bound}
4428 library.\footnote{Available at
4429 \url{http://hackage.haskell.org/package/bound}.} We describe the
4430 advantages of \texttt{bound}'s approach, but also its pitfalls in the
4431 previously relatively unknown territory of dependent
4432 types---\texttt{bound} being created mostly to handle more simply typed
4435 \texttt{bound} builds on the work of \cite{Bird1999}, who suggested to
4436 parametrising the term type over the type of the variables, and `nest'
4437 the type each time we enter a scope. If we wanted to define a term
4438 for the untyped $\lambda$-calculus, we might have
4440 -- A type with no members.
4443 data Var v = Bound | Free v
4446 = V v -- Bound variable
4447 | App (Tm v) (Tm v) -- Term application
4448 | Lam (Tm (Var v)) -- Abstraction
4450 Closed terms would be of type \texttt{Tm Empty}, so that there would be
4451 no occurrences of \texttt{V}. However, inside an abstraction, we can
4452 have \texttt{V Bound}, representing the bound variable, and inside a
4453 second abstraction we can have \texttt{V Bound} or \texttt{V (Free
4454 Bound)}. Thus the term
4455 \[\myabs{\myb{x}}{\myabs{\myb{y}}{\myb{x}}}\]
4456 can be represented as
4458 -- The top level term is of type `Tm Empty'.
4459 -- The inner term `Lam (Free Bound)' is of type `Tm (Var Empty)'.
4460 -- The second inner term `V (Free Bound)' is of type `Tm (Var (Var
4462 Lam (Lam (V (Free Bound)))
4464 This allows us to reflect the `nestedness' of a type at the type level,
4465 and since we usually work with functions polymorphic on the parameter
4466 \texttt{v} it's very hard to make mistakes by putting terms of the wrong
4467 nestedness where they do not belong.
4469 Even more interestingly, the substitution operation is perfectly
4470 captured by the \verb|>>=| (bind) operator of the \texttt{Monad}
4475 (>>=) :: m a -> (a -> m b) -> m b
4477 instance Monad Tm where
4478 -- `return'ing turns a variable into a `Tm'
4481 -- `t >>= f' takes a term `t' and a mapping from variables to terms
4482 -- `f' and applies `f' to all the variables in `t', replacing them
4483 -- with the mapped terms.
4485 App m n >>= f = App (m >>= f) (n >>= f)
4487 -- `Lam' is the tricky case: we modify the function to work with bound
4488 -- variables, so that if it encounters `Bound' it leaves it untouched
4489 -- (since the mapping refers to the outer scope); if it encounters a
4490 -- free variable it asks `f' for the term and then updates all the
4491 -- variables to make them refer to the outer scope they were meant to
4493 Lam s >>= f = Lam (s >>= bump)
4494 where bump Bound = return Bound
4495 bump (Free v) = f v >>= V . Free
4497 With this in mind, we can define functions which will not only work on
4498 \verb|Tm|, but on any \verb|Monad|!
4500 -- Replaces free variable `v' with `m' in `n'.
4501 subst :: (Eq v, Monad m) => v -> m v -> m v -> m v
4502 subst v m n = n >>= \v' -> if v == v' then m else return v'
4504 -- Replace the variable bound by `s' with term `t'.
4505 inst :: Monad m => m v -> m (Var v) -> m v
4506 inst t s = s >>= \v -> case v of
4508 Free v' -> return v'
4510 The beauty of this technique is that with a few simple functions we have
4511 defined all the core operations in a general and `obviously correct'
4512 way, with the extra confidence of having the type checker looking our
4513 back. For what concerns term equality, we can just ask the Haskell
4514 compiler to derive the instance for the \verb|Eq| type class and since
4515 we are nameless that will be enough (modulo fancy quotation).
4517 Moreover, if we take the top level term type to be \verb|Tm String|, we
4518 get a representation of terms with top-level definitions; where closed
4519 terms contain only \verb|String| references to said definitions---see
4520 also \cite{McBride2004b}.
4522 What are then the pitfalls of this seemingly invincible technique? The
4523 most obvious impediment is the need for polymorphic recursion.
4524 Functions traversing terms parametrised by the variable type will have
4527 -- Infer the type of a term, or return an error.
4528 infer :: Tm v -> Either Error (Tm v)
4530 When traversing under a \verb|Scope| the parameter changes from \verb|v|
4531 to \verb|Var v|, and thus if we do not specify the type for our function explicitly
4532 inference will fail---type inference for polymorphic recursion being
4533 undecidable \citep{henglein1993type}. This causes some annoyance,
4534 especially in the presence of many local definitions that we would like
4537 But the real issue is the fact that giving a type parametrised over a
4538 variable---say \verb|m v|---a \verb|Monad| instance means being able to
4539 only substitute variables for values of type \verb|m v|. This is a
4540 considerable inconvenience. Consider for instance the case of
4541 telescopes, which are a central tool to deal with contexts and other
4542 constructs. In Haskell we can give them a faithful representation
4543 with a data type along the lines of
4545 data Tele m v = Empty (m v) | Bind (m v) (Tele m (Var v))
4546 type TeleTm = Tele Tm
4548 The problem here is that what we want to substitute for variables in
4549 \verb|Tele m v| is \verb|m v| (probably \verb|Tm v|), not \verb|Tele m v| itself! What we need is
4551 bindTele :: Monad m => Tele m a -> (a -> m b) -> Tele m b
4552 substTele :: (Eq v, Monad m) => v -> m v -> Tele m v -> Tele m v
4553 instTele :: Monad m => m v -> Tele m (Var v) -> Tele m v
4555 Not what \verb|Monad| gives us. Solving this issue in an elegant way
4556 has been a major sink of time and source of headaches for the author,
4557 who analysed some of the alternatives---most notably the work by
4558 \cite{weirich2011binders}---but found it impossible to give up the
4559 simplicity of the model above.
4561 That said, our term type is still reasonably brief, as shown in full in
4562 Appendix \ref{app:termrep}. The fact that propositions cannot be
4563 factored out in another data type is an instance of the problem
4564 described above. However the real pain is during elaboration, where we
4565 are forced to treat everything as a type while we would much rather have
4566 telescopes. Future work would include writing a library that marries
4567 more flexibility with a nice interface similar to the one of
4570 We also make use of a `forgetful' data type (as provided by
4571 \verb|bound|) to store user-provided variables names along with the
4572 `nameless' representation, so that the names will not be considered when
4573 compared terms, but will be available when distilling so that we can
4574 recover variable names that are as close as possible to what the user
4577 \subsubsection{Evaluation}
4579 Another source of contention related to term representation is dealing
4580 with evaluation. Here \mykant\ does not make bold moves, and simply
4581 employs substitution. When type checking we match types by reducing
4582 them to their weak head normal form, as to avoid unnecessary evaluation.
4584 We treat data types eliminators and record projections in an uniform
4585 way, by elaborating declarations in a series of \emph{rewriting rules}:
4589 Tm v -> -- Term to which the destructor is applied
4590 [Tm v] -> -- List of other arguments
4591 -- The result of the rewriting, if the eliminator reduces.
4594 A rewriting rule is polymorphic in the variable type, guaranteeing that
4595 it just pattern matches on terms structure and rearranges them in some
4596 way, and making it possible to apply it at any level in the term. When
4597 reducing a series of applications we match the first term and check if
4598 it is a destructor, and if that's the case we apply the reduction rule
4599 and reduce further if it yields a new list of terms.
4601 This has the advantage of simplicity, at the expense of being quite poor
4602 in terms of performance and that we need to do quotation manually. An
4603 alternative that solves both of these is the already mentioned
4604 \emph{normalization by evaluation}, where we would compute by turning
4605 terms into Haskell values, and then reify back to terms to compare
4606 them---a useful tutorial on this technique is given by \cite{Loh2010}.
4608 However, quotation has its disadvantages. The most obvious one is that
4609 it is less simple: we need to set up some infrastructure to handle the
4610 quotation and reification, while with substitution we have a uniform
4611 representation through the process of type checking. The second is that
4612 performance advantages can be rendered less effective by the continuous
4613 quoting and reifying, although this can probably be mitigated with some
4616 \subsubsection{Parametrise everything!}
4619 Through the life of a REPL cycle we need to execute two broad
4620 `effectful' actions:
4622 \item Retrieve, add, and modify elements to an environment. The
4623 environment will contain not only types, but also the rewriting rules
4624 presented in the previous section, and a counter to generate fresh
4625 references for the type hierarchy.
4627 \item Throw various kinds of errors when something goes wrong: parsing,
4628 type checking, input/output error when reading files, and more.
4630 Haskell taught us the value of monads in programming languages, and in
4631 \mykant\ we keep this lesson in mind. All of the plumbing required to do
4632 the two actions above is provided by a very general \emph{monad
4633 transformer} that we use through the codebase, \texttt{KMonadT}:
4635 newtype KMonad f v m a = KMonad (StateT (f v) (ErrorT KError m) a)
4643 Without delving into the details of what a monad transformer
4645 \url{https://en.wikibooks.org/wiki/Haskell/Monad_transformers.}} this
4646 is what \texttt{KMonadT} works with and provides:
4648 \item The \verb|v| parameter represents the parametrised variable for
4649 the term type that we spoke about at the beginning of this section.
4652 \item The \verb|f| parameter indicates what kind of environment we are
4653 holding. Sometimes we want to traverse terms without carrying the
4654 entire environment, for various reasons---\texttt{KMonatT} lets us do
4655 that. Note that \verb|f| is itself parametrised over \verb|v|. The
4656 inner \verb|StateT| monad transformer lets us retrieve and modify this
4657 environment at any time.
4659 \item The \verb|m| is the `inner' monad that we can `plug in' to be able
4660 to perform more effectful actions in \texttt{KMonatT}. For example if we
4661 plug the \texttt{IO} monad in, we will be able to do input/output.
4663 \item The inner \verb|ErrorT| lets us throw errors at any time. The
4664 error type is \verb|KError|, which describes all the possible errors
4665 that a \mykant\ process can throw.
4667 \item Finally, the \verb|a| parameter represents the return type of the
4668 computation we are executing.
4671 The clever trick in \texttt{KMonadT} is to have it to be parametrised
4672 over the same type as the term type. This way, we can easily carry the
4673 environment while traversing under binders. For example, if we only
4674 needed to carry types of bound variables in the environment, we can
4675 quickly set up the following infrastructure:
4679 -- A context is a mapping from variables to types.
4680 newtype Ctx v = Ctx (v -> Tm v)
4682 -- A context monad holds a context.
4683 type CtxMonad v m = KMonadT Ctx v m
4685 -- Enter into a scope binding a type to the variable, execute a
4686 -- computation there, and return exit the scope returning to the `current'
4688 nestM :: Monad m => Tm v -> CtxMonad (Var v) m a -> CtxMonad v m a
4691 Again, the types guard our back guaranteeing that we add a type when we
4692 enter a scope, and we discharge it when we get out. The author
4693 originally started with a more traditional representation and often
4694 forgot to add the right variable at the right moment. Using this
4695 practices it is very difficult to do so---we achieve correctness through
4698 In the actual \mykant\ codebase, we have also abstracted the concept of
4699 `context' further, so that we can easily embed contexts into other
4700 structures and write generic operations on all context-like
4701 structures.\footnote{See the \texttt{Kant.Cursor} module for details.}
4703 \subsection{Turning a hierarchy into some graphs}
4704 \label{sec:hier-impl}
4706 In this section we will explain how to implement the typical ambiguity
4707 we have spoken about in \ref{sec:term-hierarchy} efficiently, a subject
4708 which is often dismissed in the literature. As mentioned, we have to
4709 verify a the consistency of a set of constraints each time we add a new
4710 one. The constraints range over some set of variables whose members we
4711 will denote with $x, y, z, \dots$. and are of two kinds:
4718 Predictably, $\le$ expresses a reflexive order, and $<$ expresses an
4719 irreflexive order, both working with the same notion of equality, where
4720 $x < y$ implies $x \le y$---they behave like $\le$ and $<$ do for natural
4721 numbers (or in our case, levels in a type hierarchy). We also need an
4722 equality constraint ($x = y$), which can be reduced to two constraints
4723 $x \le y$ and $y \le x$.
4725 Given this specification, we have implemented a standalone Haskell
4726 module---that we plan to release as a library---to efficiently store and
4727 check the consistency of constraints. The problem predictably reduces
4728 to a graph algorithm, and for this reason we also implement a library
4729 for labelled graphs, since the existing Haskell graph libraries fell
4730 short in different areas.\footnote{We tried the \texttt{Data.Graph}
4731 module in \url{http://hackage.haskell.org/package/containers}, and the
4732 much more featureful \texttt{fgl} library
4733 \url{http://hackage.haskell.org/package/fgl}.} The interfaces for
4734 these modules are shown in Appendix \ref{app:constraint}. The graph
4735 library is implemented as a modification of the code described by
4738 We represent the set by building a graph where vertices are variables,
4739 and edges are constraints between them, labelled with the appropriate
4740 constraint: $x < y$ gives rise to a $<$-labelled edge from $x$ to $y$,
4741 and $x \le y$ to a $\le$-labelled edge from $x$ to $y$. As we add
4742 constraints, $\le$ constraints are replaced by $<$ constraints, so that
4743 if we started with an empty set and added
4745 x < y,\ y \le z,\ z \le k,\ k < j,\ j \le y\
4747 it would generate the graph shown in Figure \ref{fig:graph-one-before},
4748 but adding $z < k$ would strengthen the edge from $z$ to $k$, as shown
4749 in \ref{fig:graph-one-after}.
4753 \begin{subfigure}[b]{0.3\textwidth}
4754 \begin{tikzpicture}[node distance=1.5cm]
4757 \node [right of=x] (y) {$y$};
4758 \node [right of=y] (z) {$z$};
4759 \node [below of=z] (k) {$k$};
4760 \node [left of=k] (j) {$j$};
4763 (x) edge node [above] {$<$} (y)
4764 (y) edge node [above] {$\le$} (z)
4765 (z) edge node [right] {$\le$} (k)
4766 (k) edge node [below] {$\le$} (j)
4767 (j) edge node [left ] {$\le$} (y);
4769 \caption{Before $z < k$.}
4770 \label{fig:graph-one-before}
4773 \begin{subfigure}[b]{0.3\textwidth}
4774 \begin{tikzpicture}[node distance=1.5cm]
4777 \node [right of=x] (y) {$y$};
4778 \node [right of=y] (z) {$z$};
4779 \node [below of=z] (k) {$k$};
4780 \node [left of=k] (j) {$j$};
4783 (x) edge node [above] {$<$} (y)
4784 (y) edge node [above] {$\le$} (z)
4785 (z) edge node [right] {$<$} (k)
4786 (k) edge node [below] {$\le$} (j)
4787 (j) edge node [left ] {$\le$} (y);
4789 \caption{After $z < k$.}
4790 \label{fig:graph-one-after}
4793 \begin{subfigure}[b]{0.3\textwidth}
4794 \begin{tikzpicture}[remember picture, node distance=1.5cm]
4795 \begin{pgfonlayer}{foreground}
4798 \node [right of=x] (y) {$y$};
4799 \node [right of=y] (z) {$z$};
4800 \node [below of=z] (k) {$k$};
4801 \node [left of=k] (j) {$j$};
4804 (x) edge node [above] {$<$} (y)
4805 (y) edge node [above] {$\le$} (z)
4806 (z) edge node [right] {$<$} (k)
4807 (k) edge node [below] {$\le$} (j)
4808 (j) edge node [left ] {$\le$} (y);
4809 \end{pgfonlayer}{foreground}
4811 \begin{tikzpicture}[remember picture, overlay]
4812 \begin{pgfonlayer}{background}
4813 \fill [red, opacity=0.3, rounded corners]
4814 (-2.7,2.6) rectangle (-0.2,0.05)
4815 (-4.1,2.4) rectangle (-3.3,1.6);
4816 \end{pgfonlayer}{background}
4819 \label{fig:graph-one-scc}
4821 \caption{Strong constraints overrule weak constraints.}
4822 \label{fig:graph-one}
4825 \begin{mydef}[Strongly connected component]
4826 A \emph{strongly connected component} in a graph with vertices $V$ is
4827 a subset of $V$, say $V'$, such that for each $(v_1,v_2) \in V' \times
4828 V$ there is a path from $v_1$ to $v_2$.
4831 The SCCs in the graph for the constraints above is shown in Figure
4832 \ref{fig:graph-one-scc}. If we have a strongly connected component with
4833 a $<$ edge---say $x < y$---in it, we have an inconsistency, since there
4834 must also be a path from $y$ to $x$, and by transitivity it must either
4835 be the case that $y \le x$ or $y < x$, which are both at odds with $x <
4838 Moreover, if we have a SCC with no $<$ edges, it means that all members
4839 of said SCC are equal, since for every $x \le y$ we have a path from $y$
4840 to $x$, which again by transitivity means that $y \le x$. Thus, we can
4841 \emph{condense} the SCC to a single vertex, by choosing a variable among
4842 the SCC as a representative for all the others. This can be done
4843 efficiently with disjoint set data structure, and is crucial to keep the
4844 graph compact, given the very large number of constraints generated when
4847 \subsection{(Web) REPL}
4849 Finally, we take a break from the types by giving a brief account of the
4850 design of our REPL, being a good example of modular design using various
4851 constructs dear to the Haskell programmer.
4853 Keeping in mind the \texttt{KMonadT} monad described in Section
4854 \ref{sec:parame}, the REPL is represented as a function in
4855 \texttt{KMonadT} consuming input and hopefully producing output. Then,
4856 frontends can very easily written by marshalling data in and out of the
4860 = ITyCheck String -- Type check a term
4861 | IEval String -- Evaluate a term
4862 | IDecl String -- Declare something
4866 = OTyCheck TmRefId [HoleCtx] -- Type checked term, with holes
4867 | OPretty TmRefId -- Term to pretty print, after evaluation
4868 -- Just holes, classically after loading a file
4872 -- KMonadT is parametrised over the type of the variables, which depends
4873 -- on how deep in the term structure we are. For the REPL, we only deal
4874 -- with top-level terms, and thus only `Id' variables---top level names.
4875 type REPL m = KMonadT Id m
4877 repl :: ReadFile m => Input -> REPL m Output
4880 The \texttt{ReadFile} monad embodies the only `extra' action that we
4881 need to have access too when running the REPL: reading files. We could
4882 simply use the \texttt{IO} monad, but this will not serve us well when
4883 implementing front end facing untrusted parties accessing the application
4884 running on our servers. In our case we expose the REPL as a web
4885 application, and we want the user to be able to load only from a
4886 pre-defined directory, not from the entire file system.
4888 For this reason we specify \texttt{ReadFile} to have just one function:
4890 class Monad m => ReadFile m where
4891 readFile' :: FilePath -> m (Either IOError String)
4893 While in the command-line application we will use the \texttt{IO} monad
4894 and have \texttt{readFile'} to work in the `obvious' way---by reading
4895 the file corresponding to the given file path---in the web prompt we
4896 will have it to accept only a file name, not a path, and read it from a
4897 pre-defined directory:
4899 -- The monad that will run the web REPL. The `ReaderT' holds the
4900 -- filepath to the directory where the files loadable by the user live.
4901 -- The underlying `IO' monad will be used to actually read the files.
4902 newtype DirRead a = DirRead (ReaderT FilePath IO a)
4904 instance ReadFile DirRead where
4906 do -- We get the base directory in the `ReaderT' with `ask'
4908 -- Is the filepath provided an unqualified file name?
4909 if snd (splitFileName fp) == fp
4910 -- If yes, go ahead and read the file, by lifting
4911 -- `readFile'' into the IO monad
4912 then DirRead (lift (readFile' (dir </> fp)))
4913 -- If not, return an error
4914 else return (Left (strMsg ("Invalid file name `" ++ fp ++ "'")))
4916 Once this light-weight infrastructure is in place, adding a web
4917 interface was an easy exercise. We use Jasper Van der Jeugt's
4918 \texttt{websockets} library\footnote{Available at
4919 \url{http://hackage.haskell.org/package/websockets}.} to create a
4920 proxy that receives \texttt{JSON}\footnote{\texttt{JSON} is a popular data interchange
4921 format, see \url{http://json.org} for more info.} messages with the
4922 user input, turns them into \texttt{Input} messages for the REPL, and
4923 then sends back a \texttt{JSON} message with the response. Moreover, each client
4924 is handled in a separate threads, so crashes of the REPL for a certain
4925 client will not bring the whole application down.
4927 On the frontend side, we had to write some JavaScript to accept input
4928 from a form, and to make the responses appear on the screen. The web
4929 prompt is publicly available at \url{http://bertus.mazzo.li}, a sample
4930 session is shown Figure \ref{fig:web-prompt-one}.
4933 \includegraphics[width=\textwidth]{web-prompt.png}
4934 \caption{A sample run of the web prompt.}
4935 \label{fig:web-prompt-one}
4940 \section{Evaluation}
4941 \label{sec:evaluation}
4943 Going back to our goals in Section \ref{sec:contributions}, we feel that
4944 this thesis fills a gap in the description of observational type theory.
4945 In the design of \mykant\ we willingly patterned the core features
4946 against the ones present in Agda, with the hope that future implementors
4947 will be able to refer to this document without embarking on the same
4948 adventure themselves. We gave an original account of heterogeneous
4949 equality by showing that in a cumulative hierarchy we can keep
4950 equalities as small as we would be able too with a separate notion of
4951 type equality. As a side effect of developing \mykant, we also gave an
4952 original account of bidirectional type checking for user defined types,
4953 which get rid of many types while keeping the language very simple.
4955 Through the design of the theory of \mykant\ we have followed an
4956 approach where study and implementation were continuously interleaved,
4957 as a `reality check' for the ideas that we wished to implement. Given
4958 the great effort necessary to build a theorem prover capable of
4959 `real-world' proofs we have not attempted to compare \mykant's
4960 capabilities to those of Agda and Coq, the theorem provers that the
4961 author is most familiar with and in general two of the main players in
4962 the field. However we have ported a lot of simpler examples to check
4963 that the key features are working, some of which have been used in the
4964 previous sections and are reproduced in the appendices\footnote{The full
4965 list is available in the repository:
4966 \url{https://github.com/bitonic/kant/tree/master/data/samples/good}.}.
4967 A full example of interaction with \mykant\ is given in Section
4968 \ref{sec:type-holes}.
4970 The main culprits for the delays in the implementation are two issues
4971 that revealed themselves to be far less obvious than what the author
4972 predicted. The first, as we have already remarked in Section
4973 \ref{sec:term-repr}, is to have an adequate term representation that
4974 lets us express the right constructs in a safe way. There is still no
4975 widely accepted solution to this problem, which is approached in many
4976 different ways both in the literature and in the programming
4977 practice. The second aspect is the treatment of user defined data types.
4978 Again, the best techniques to implement them in a dependently typed
4979 setting still have not crystallised and implementors reinvent many
4980 wheels each time a new system is built. The author is still conflicted
4981 on whether having user defined types at all it is the right decision:
4982 while they are essential, the recent discovery of a paper by
4983 \cite{dagand2012elaborating} describing a way to efficiently encode
4984 user-defined data types to a set of core primitives---an option that
4985 seems very attractive.
4987 In general, implementing dependently typed languages is still a poorly
4988 understood practice, and almost every stage requires experimentation on
4989 behalf of the author. Another example is the treatment of the implicit
4990 hierarchy, where no resources are present describing the problem from an
4991 implementation perspective (we described our approach in Section
4992 \ref{sec:hier-impl}). Hopefully this state of things will change in the
4993 near future, and recent publications are promising in this direction,
4994 for example an unpublished paper by \cite{Brady2013} describing his
4995 implementation of the Idris programming language. Our ultimate goal is
4996 to be a part of this collective effort.
4998 \subsection{A type holes tutorial}
4999 \label{sec:type-holes}
5001 As a taster and showcase for the capabilities of \mykant, we present an
5002 interactive session with the \mykant\ REPL. While doing so, we present
5003 a feature that we still have not covered: type holes.
5005 Type holes are, in the author's opinion, one of the `killer' features of
5006 interactive theorem provers, and one that is begging to be exported to
5007 mainstream programming---although it is much more effective in a
5008 well-typed, functional setting. The idea is that when we are developing
5009 a proof or a program we can insert a hole to have the software tell us
5010 the type expected at that point. Furthermore, we can ask for the type
5011 of variables in context, to better understand our surroundings.
5013 In \mykant\ we use type holes by putting them where a term should go.
5014 We need to specify a name for the hole and then we can put as many terms
5015 as we like in it. \mykant\ will tell us which type it is expecting for
5016 the term where the hole is, and the type for each term that we have
5017 included. For example if we had:
5019 plus [m n : Nat] : Nat ⇒ (
5023 And we loaded the file in \mykant, we would get:
5024 \begin{Verbatim}[frame=leftline]
5032 Suppose we wanted to define the `less or equal' ordering on natural
5033 numbers as described in Section \ref{sec:user-type}. We will
5034 incrementally build our functions in a file called \texttt{le.ka}.
5035 First we define the necessary types, all of which we know well by now:
5037 data Nat : ⋆ ⇒ { zero : Nat | suc : Nat → Nat }
5039 data Empty : ⋆ ⇒ { }
5040 absurd [A : ⋆] [p : Empty] : A ⇒ (
5041 Empty-Elim p (λ _ ⇒ A)
5044 record Unit : ⋆ ⇒ tt { }
5046 Then fire up \mykant, and load the file:
5047 \begin{Verbatim}[frame=leftline]
5050 Version 0.0, made in London, year 2013.
5054 So far so good. Our definition will be defined by recursion on a
5055 natural number \texttt{n}, which will return a function that given
5056 another number \texttt{m} will return the trivial type \texttt{Unit} if
5057 $\texttt{n} \le \texttt{m}$, and the \texttt{Empty} type otherwise.
5058 However we are still not sure on how to define it, so we invoke
5059 $\texttt{Nat-Elim}$, the eliminator for natural numbers, and place holes
5060 instead of arguments. In the file we will write:
5062 le [n : Nat] : Nat → ⋆ ⇒ (
5063 Nat-Elim n (λ _ ⇒ Nat → ⋆)
5068 And then we reload in \mykant:
5069 \begin{Verbatim}[frame=leftline]
5073 h2 : Nat → (Nat → ⋆) → Nat → ⋆
5075 Which tells us what types we need to satisfy in each hole. However, it
5076 is not that clear what does what in each hole, and thus it is useful to
5077 have a definition vacuous in its arguments just to clear things up. We
5078 will use \texttt{Le} aid in reading the goal, with \texttt{Le m n} as a
5079 reminder that we to return the type corresponding to $\texttt{m} ≤
5082 Le [m n : Nat] : ⋆ ⇒ ⋆
5084 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5085 Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5090 \begin{Verbatim}[frame=leftline]
5093 h1 : [m : Nat] → Le zero m
5094 h2 : [x : Nat] → ([m : Nat] → Le x m) → [m : Nat] → Le (suc x) m
5096 This is much better! \mykant, when printing terms, does not substitute
5097 top-level names for their bodies, since usually the resulting term is
5098 much clearer. As a nice side-effect, we can use tricks like this to
5101 In this case in the first case we need to return, given any number
5102 \texttt{m}, $0 \le \texttt{m}$. The trivial type will do, since every
5103 number is less or equal than zero:
5105 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5106 Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5111 \begin{Verbatim}[frame=leftline]
5114 h2 : [x : Nat] → ([m : Nat] → Le x m) → [m : Nat] → Le (suc x) m
5116 Now for the important case. We are given our comparison function for a
5117 number, and we need to produce the function for the successor. Thus, we
5118 need to re-apply the induction principle on the other number, \texttt{m}:
5120 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5121 Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5123 (λ n' f m ⇒ Nat-Elim m (λ m' ⇒ Le (suc n') m') {|h2|} {|h3|})
5126 \begin{Verbatim}[frame=leftline]
5130 h3 : [x : Nat] → Le (suc n') x → Le (suc n') (suc x)
5132 In the first hole we know that the second number is zero, and thus we
5133 can return empty. In the second case, we can use the recursive argument
5134 \texttt{f} on the two numbers:
5136 le [n : Nat] : [m : Nat] → Le n m ⇒ (
5137 Nat-Elim n (λ n ⇒ [m : Nat] → Le n m)
5140 Nat-Elim m (λ m' ⇒ Le (suc n') m') Empty (λ f _ ⇒ f m'))
5143 We can verify that our function works as expected:
5144 \begin{Verbatim}[frame=leftline]
5147 >>> :e le zero (suc zero)
5149 >>> :e le (suc (suc zero)) (suc zero)
5152 The other functionality of type holes is examining types of things in
5153 context. Going back to the examples in Section \ref{sec:term-types}, we can
5154 implement the safe \texttt{head} function with our newly defined
5157 data List : [A : ⋆] → ⋆ ⇒
5158 { nil : List A | cons : A → List A → List A }
5160 length [A : ⋆] [l : List A] : Nat ⇒ (
5161 List-Elim l (λ _ ⇒ Nat) zero (λ _ _ n ⇒ suc n)
5164 gt [n m : Nat] : ⋆ ⇒ (le (suc m) n)
5166 head [A : ⋆] [l : List A] : gt (length A l) zero → A ⇒ (
5167 List-Elim l (λ l ⇒ gt (length A l) zero → A)
5172 We define \texttt{List}s, a polymorphic \texttt{length} function, and
5173 express $<$ (\texttt{gt}) in terms of $\le$. Then, we set up the type
5174 for our \texttt{head} function. Given a list and a proof that its
5175 length is greater than zero, we return the first element. If we load
5176 this in \mykant, we get:
5177 \begin{Verbatim}[frame=leftline]
5182 h2 : [x : A] [x1 : List A] →
5183 (gt (length A x1) zero → A) →
5184 gt (length A (cons x x1)) zero → A
5186 In the first case (the one for \texttt{nil}), we have a proof of
5187 \texttt{Empty}---surely we can use \texttt{absurd} to get rid of that
5188 case. In the second case we simply return the element in the
5191 head [A : ⋆] [l : List A] : gt (length A l) zero → A ⇒ (
5192 List-Elim l (λ l ⇒ gt (length A l) zero → A)
5197 Now, if we tried to get the head of an empty list, we face a problem:
5198 \begin{Verbatim}[frame=leftline]
5202 We would have to provide something of type \texttt{Empty}, which
5203 hopefully should be impossible. For non-empty lists, on the other hand,
5204 things run smoothly:
5205 \begin{Verbatim}[frame=leftline]
5206 >>> :t head Nat (cons zero nil)
5208 >>> :e head Nat (cons zero nil) tt
5211 This should give a vague idea of why type holes are so useful and in
5212 more in general about the development process in \mykant. Most
5213 interactive theorem provers offer some kind of facility
5214 to... interactively develop proofs, usually much more powerful than the
5215 fairly bare tools present in \mykant. Agda in particular offers a
5216 celebrated interactive mode for the \texttt{Emacs} text editor.
5218 \section{Future work}
5219 \label{sec:future-work}
5221 The first move that the author plans to make is to work towards a simple
5222 but powerful term representation. A good plan seems to be to associate
5223 each type (terms, telescopes, etc.) with what we can substitute
5224 variables with, so that the term type will be associated with itself,
5225 while telescopes and propositions will be associated to terms. This can
5226 probably be accomplished elegantly with Haskell's \emph{type families}
5227 \citep{chakravarty2005associated}. After achieving a more solid
5228 machinery for terms, implementing observational equality fully should
5229 prove relatively easy.
5231 Beyond this steps, we can go in many directions to improve the
5232 system that we described---here we review the main ones.
5235 \item[Pattern matching and recursion] Eliminators are very clumsy,
5236 and using them can be especially frustrating if we are used to writing
5237 functions via explicit recursion. \cite{Gimenez1995} showed how to
5238 reduce well-founded recursive definitions to primitive recursors.
5239 Intuitively, defining a function through an eliminators corresponds to
5240 pattern matching and recursively calling the function on the recursive
5241 occurrences of the type we matched against.
5243 Nested pattern matching can be justified by identifying a notion of
5244 `structurally smaller', and allowing recursive calls on all smaller
5245 arguments. Epigram goes all the way and actually implements recursion
5246 exclusively by providing a convenient interface to the two constructs
5247 above \citep{EpigramTut, McBride2004}.
5249 However as we extend the flexibility in our recursion elaborating
5250 definitions to eliminators becomes more and more laborious. For
5251 example we might want mutually recursive definitions and definitions
5252 that terminate relying on the structure of two arguments instead of
5253 just one. For this reason both Agda and Coq (Agda putting more
5254 effort) let the user write recursive definitions freely, and then
5255 employ an external syntactic one the recursive calls to ensure that
5256 the definitions are terminating.
5258 Moreover, if we want to use dependently typed languages for
5259 programming purposes, we will probably want to sidestep the
5260 termination checker and write a possibly non-terminating function;
5261 maybe because proving termination is particularly difficult. With
5262 explicit recursion this amounts to turning off a check, if we have
5263 only eliminators it is impossible.
5265 \item[More powerful data types] A popular improvement on basic data
5266 types are inductive families \citep{Dybjer1991}, where the parameters
5267 for the type constructors can change based on the data constructors,
5268 which lets us express naturally types such as $\mytyc{Vec} : \mynat
5269 \myarr \mytyp$, which given a number returns the type of lists of that
5270 length, or $\mytyc{Fin} : \mynat \myarr \mytyp$, which given a number
5271 $n$ gives the type of numbers less than $n$. This apparent omission
5272 was motivated by the fact that inductive families can be represented
5273 by adding equalities concerning the parameters of the type
5274 constructors as arguments to the data constructor, in much the same
5275 way that Generalised Abstract Data Types \citep{GHC} are handled in
5276 Haskell. Interestingly the modified version of System F that lies at
5277 the core of recent versions of GHC features coercions reminiscent of
5278 those found in OTT, motivated precisely by the need to implement GADTs
5279 in an elegant way \citep{Sulzmann2007}.
5281 Another concept introduced by \cite{dybjer2000general} is
5282 induction-recursion, where we define a data type in tandem with a
5283 function on that type. This technique has proven extremely useful to
5284 define embeddings of other calculi in an host language, by defining
5285 the representation of the embedded language as a data type and at the
5286 same time a function decoding from the representation to a type in the
5287 host language. The decoding function is then used to define the data
5288 type for the embedding itself, for example by reusing the host's
5289 language functions to describe functions in the embedded language,
5290 with decoded types as arguments.
5292 It is also worth mentioning that in recent times there has been work
5293 \citep{dagand2012elaborating, chapman2010gentle} to show how to define
5294 a set of primitives that data types can be elaborated into. The big
5295 advantage of the approach proposed is enabling a very powerful notion
5296 of generic programming, by writing functions working on the
5297 `primitive' types as to be workable by all the other `compatible'
5298 elaborated user defined types. This has been a considerable problem
5299 in the dependently type world, where we often define types which are
5300 more `strongly typed' version of similar structures,\footnote{For
5301 example the $\mytyc{OList}$ presented in Section \ref{sec:user-type}
5302 being a `more typed' version of an ordinary list.} and then find
5303 ourselves forced to redefine identical operations on both types.
5305 \item[Pattern matching and inductive families] The notion of inductive
5306 family also yields a more interesting notion of pattern matching,
5307 since matching on an argument influences the value of the parameters
5308 of the type of said argument. This means that pattern matching
5309 influences the context, which can be exploited to constraint the
5310 possible data constructors for \emph{other} arguments
5311 \citep{McBride2004}.
5313 \item[Type inference] While bidirectional type checking helps at a very
5314 low cost of implementation and complexity, a much more powerful weapon
5315 is found in \emph{pattern unification}, which allows Hindley-Milner
5316 style inference for dependently typed languages. Unification for
5317 higher order terms is undecidable and unification problems do not
5318 always have a most general unifier \citep{huet1973undecidability}.
5319 However \cite{miller1992unification} identified a decidable fragment
5320 of higher order unification commonly known as pattern unification,
5321 which is employed in most theorem provers to drastically reduce the
5322 number of type annotations. \cite{gundrytutorial} provide a tutorial
5325 \item[Coinductive data types] When we specify inductive data types, we
5326 do it by specifying its \emph{constructors}---functions with the type
5327 we are defining as codomain. Then, we are offered way of compute by
5328 recursively \emph{destructing} or \emph{eliminating} a member of the
5331 Coinductive data types are the dual of this approach. We specify ways
5332 to destruct data, and we are given a way to generate the defined type
5333 by repeatedly `unfolding' starting from some seed data. For example,
5334 we could defined infinite streams by specifying a $\myfun{head}$ and
5335 $\myfun{tail}$ destructors---here using a syntax reminiscent of
5339 \mysyn{codata}\ \mytyc{Stream}\myappsp (\myb{A} {:} \mytyp)\ \mysyn{where} \\
5340 \myind{2} \{ \myfun{head} : \myb{A}, \myfun{tail} : \mytyc{Stream} \myappsp \myb{A}\}
5343 which will hopefully give us something like
5346 \myfun{head} : (\myb{A}{:}\mytyp) \myarr \mytyc{Stream} \myappsp \myb{A} \myarr \myb{A} \\
5347 \myfun{tail} : (\myb{A}{:}\mytyp) \myarr \mytyc{Stream} \myappsp \myb{A} \myarr \mytyc{Stream} \myappsp \myb{A} \\
5348 \mytyc{Stream}.\mydc{unfold} : (\myb{A}\, \myb{B} {:} \mytyp) \myarr (\myb{A} \myarr \myb{B} \myprod \myb{A}) \myarr \myb{A} \myarr \mytyc{Stream} \myappsp \myb{B}
5351 Where, in $\mydc{unfold}$, $\myb{B} \myprod \myb{A}$ represents the
5352 fields of $\mytyc{Stream}$ but with the recursive occurrence replaced
5353 by the `seed' type $\myb{A}$.
5355 Beyond simple infinite types like $\mytyc{Stream}$, coinduction is
5356 particularly useful to write non-terminating programs like servers or
5357 software interacting with a user, while guaranteeing their liveliness.
5358 Moreover it lets us model possibly non-terminating computations in an
5359 elegant way \citep{Capretta2005}, enabling for example the study of
5360 operational semantics for non-terminating languages
5361 \citep{Danielsson2012}.
5363 \cite{cockett1992charity} pioneered this approach in their programming
5364 language Charity, and coinduction has since been adopted in systems
5365 such as Coq \citep{Gimenez1996} and Agda. However these
5366 implementations are unsatisfactory, since Coq's break subject
5367 reduction; and Agda, to avoid this problem, does not allow types to
5368 depend on the unfolding of codata. \cite{mcbride2009let} has shown
5369 how observational equality can help to resolve these issues, since we
5370 can reason about the unfoldings in a better way, like we reason about
5371 functions' extensional behaviour.
5374 The author looks forward to the study and possibly the implementation of
5375 these ideas in the years to come.
5381 \section{Notation and syntax}
5382 \label{app:notation}
5384 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
5385 the type of relation being established and the syntactic elements appearing,
5388 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
5389 Typing derivations here.
5392 In the languages presented and Agda code samples we also highlight the syntax,
5393 following a uniform colour, capitalisation, and font style convention:
5396 \begin{tabular}{c | l}
5397 $\mytyc{Sans}$ & Type constructors. \\
5398 $\mydc{sans}$ & Data constructors. \\
5399 % $\myfld{sans}$ & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
5400 $\mysyn{roman}$ & Keywords of the language. \\
5401 $\myfun{roman}$ & Defined values and destructors. \\
5402 $\myb{math}$ & Bound variables.
5406 When presenting grammars, we use a word in $\mysynel{math}$ font
5407 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate
5408 nonterminals. Additionally, we use quite flexibly a $\mysynel{math}$
5409 font to indicate a syntactic element in derivations or meta-operations.
5410 More specifically, terms are usually indicated by lowercase letters
5411 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase
5412 letter (often $\mytya$, $\mytyb$, or $\mytycc$).
5414 When presenting type derivations, we often abbreviate and present multiple
5415 conclusions, each on a separate line:
5417 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
5418 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
5420 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
5422 We often present `definitions' in the described calculi and in
5423 $\mykant$\ itself, like so:
5426 \myfun{name} : \mytysyn \\
5427 \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
5430 To define operators, we use a mixfix notation similar
5431 to Agda, where $\myarg$s denote arguments:
5434 \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
5435 \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
5438 In explicitly typed systems, we omit type annotations when they
5439 are obvious, e.g. by not annotating the type of parameters of
5440 abstractions or of dependent pairs.\\
5441 We introduce multiple arguments in one go in arrow types:
5443 (\myb{x}\, \myb{y} {:} \mytya) \myarr \cdots = (\myb{x} {:} \mytya) \myarr (\myb{y} {:} \mytya) \myarr \cdots
5445 and in abstractions:
5447 \myabs{\myb{x}\myappsp\myb{y}}{\cdots} = \myabs{\myb{x}}{\myabs{\myb{y}}{\cdots}}
5449 We also omit arrows to abbreviate types:
5451 (\myb{x} {:} \mytya)(\myb{y} {:} \mytyb) \myarr \cdots =
5452 (\myb{x} {:} \mytya) \myarr (\myb{y} {:} \mytyb) \myarr \cdots
5455 Meta operations names are displayed in $\mymeta{smallcaps}$ and
5456 written in a pattern matching style, also making use of boolean guards.
5457 For example, a meta operation operating on a context and terms might
5461 \mymeta{quux}(\myctx, \myb{x}) \mymetaguard \myb{x} \in \myctx \mymetagoes \myctx(\myb{x}) \\
5462 \mymeta{quux}(\myctx, \myb{x}) \mymetagoes \mymeta{outofbounds} \\
5467 From time to time we give examples in the Haskell programming
5468 language as defined by \cite{Haskell2010}, which we typeset in
5469 \texttt{teletype} font. I assume that the reader is already familiar
5470 with Haskell, plenty of good introductions are available
5471 \citep{LYAH,ProgInHask}.
5473 Examples of \mykant\ code will be typeset nicely with \LaTeX in Section
5474 \ref{sec:kant-theory}, to adjust with the rest of the presentation; and
5475 in \texttt{teletype} font in the rest of the document, including Section
5476 \ref{sec:kant-practice} and in the appendices. All the \mykant\ code
5477 shown is meant to be working and ready to be inputted in a \mykant\
5478 prompt or loaded from a file. Snippets of sessions in the \mykant\
5479 prompt will be displayed with a left border, to distinguish them from
5481 \begin{Verbatim}[frame=leftline]
5488 \subsection{ITT renditions}
5489 \label{app:itt-code}
5491 \subsubsection{Agda}
5492 \label{app:agda-itt}
5494 Note that in what follows rules for `base' types are
5495 universe-polymorphic, to reflect the exposition. Derived definitions,
5496 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
5497 that in the theory presented we don't have universe polymorphism.
5503 data Empty : Set where
5505 absurd : ∀ {a} {A : Set a} → Empty → A
5508 ¬_ : ∀ {a} → (A : Set a) → Set a
5511 record Unit : Set where
5514 record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
5521 data Bool : Set where
5524 if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
5525 if true / _ then x else _ = x
5526 if false / _ then _ else x = x
5528 if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
5529 if_then_else_ x {P} = if_/_then_else_ x P
5531 data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
5532 _◁_ : (s : S) → (P s → W S P) → W S P
5534 rec : ∀ {a b} {S : Set a} {P : S → Set b}
5535 (C : W S P → Set) → -- some conclusion we hope holds
5536 ((s : S) → -- given a shape...
5537 (f : P s → W S P) → -- ...and a bunch of kids...
5538 ((p : P s) → C (f p)) → -- ...and C for each kid in the bunch...
5539 C (s ◁ f)) → -- ...does C hold for the node?
5540 (x : W S P) → -- If so, ...
5541 C x -- ...C always holds.
5542 rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
5544 module Examples-→ where
5551 -- These pragmas are needed so we can use number literals.
5552 {-# BUILTIN NATURAL ℕ #-}
5553 {-# BUILTIN ZERO zero #-}
5554 {-# BUILTIN SUC suc #-}
5556 data List (A : Set) : Set where
5558 _∷_ : A → List A → List A
5560 length : ∀ {A} → List A → ℕ
5562 length (_ ∷ l) = suc (length l)
5567 suc x > suc y = x > y
5569 head : ∀ {A} → (l : List A) → length l > 0 → A
5570 head [] p = absurd p
5573 module Examples-× where
5579 even (suc zero) = Empty
5580 even (suc (suc n)) = even n
5585 5-not-even : ¬ (even 5)
5588 there-is-an-even-number : ℕ × even
5589 there-is-an-even-number = 6 , 6-even
5591 _∨_ : (A B : Set) → Set
5592 A ∨ B = Bool × (λ b → if b then A else B)
5594 left : ∀ {A B} → A → A ∨ B
5597 right : ∀ {A B} → B → A ∨ B
5600 [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
5602 (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
5604 module Examples-W where
5609 Tr b = if b then Unit else Empty
5615 zero = false ◁ absurd
5618 suc n = true ◁ (λ _ → n)
5624 if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
5625 then (λ _ f → (suc (f tt))) else (λ _ _ → y))
5628 module Equality where
5631 data _≡_ {a} {A : Set a} : A → A → Set a where
5634 ≡-elim : ∀ {a b} {A : Set a}
5635 (P : (x y : A) → x ≡ y → Set b) →
5636 ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
5637 ≡-elim P p (refl x) = p
5639 subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
5640 subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
5642 sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
5643 sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
5645 trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
5646 trans x y z p q = subst (λ z′ → x ≡ z′) q p
5648 cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y
5649 cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
5652 \subsubsection{\mykant}
5653 \label{app:kant-itt}
5655 The following things are missing: $\mytyc{W}$-types, since our
5656 positivity check is overly strict, and equality, since we haven't
5657 implemented that yet.
5660 \verbatiminput{itt.ka}
5663 \subsection{\mykant\ examples}
5664 \label{app:kant-examples}
5667 \verbatiminput{examples.ka}
5670 \subsection{\mykant' hierachy}
5673 This rendition of the Hurken's paradox does not type check with the
5674 hierachy enabled, type checks and loops without it. Adapted from an
5675 Agda version, available at
5676 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
5679 \verbatiminput{hurkens.ka}
5682 \subsection{Term representation}
5685 Data type for terms in \mykant.
5687 {\small\begin{verbatim}-- A top-level name.
5689 -- A data/type constructor name.
5692 -- A term, parametrised over the variable (`v') and over the reference
5693 -- type used in the type hierarchy (`r').
5696 | Ty r -- Type, with a hierarchy reference.
5697 | Lam (TmScope r v) -- Abstraction.
5698 | Arr (Tm r v) (TmScope r v) -- Dependent function.
5699 | App (Tm r v) (Tm r v) -- Application.
5700 | Ann (Tm r v) (Tm r v) -- Annotated term.
5701 -- Data constructor, the first ConId is the type constructor and
5702 -- the second is the data constructor.
5703 | Con ADTRec ConId ConId [Tm r v]
5704 -- Data destrutor, again first ConId being the type constructor
5705 -- and the second the name of the eliminator.
5706 | Destr ADTRec ConId Id (Tm r v)
5708 | Hole HoleId [Tm r v]
5709 -- Decoding of propositions.
5713 | Prop r -- The type of proofs, with hierarchy reference.
5716 | And (Tm r v) (Tm r v)
5717 | Forall (Tm r v) (TmScope r v)
5718 -- Heterogeneous equality.
5719 | Eq (Tm r v) (Tm r v) (Tm r v) (Tm r v)
5721 -- Either a data type, or a record.
5722 data ADTRec = ADT | Rec
5724 -- Either a coercion, or coherence.
5725 data Coeh = Coe | Coh\end{verbatim}
5728 \subsection{Graph and constraints modules}
5729 \label{app:constraint}
5731 The modules are respectively named \texttt{Data.LGraph} (short for
5732 `labelled graph'), and \texttt{Data.Constraint}. The type class
5733 constraints on the type parameters are not shown for clarity, unless
5734 they are meaningful to the function. In practice we use the
5735 \texttt{Hashable} type class on the vertex to implement the graph
5736 efficiently with hash maps.
5738 \subsubsection{\texttt{Data.LGraph}}
5741 \verbatiminput{graph.hs}
5744 \subsubsection{\texttt{Data.Constraint}}
5747 \verbatiminput{constraint.hs}
5752 \bibliographystyle{authordate1}
5753 \bibliography{thesis}