1 \documentclass[report]{article}
5 % \usepackage{fullpage}
17 \usepackage[fleqn]{amsmath}
18 \usepackage{stmaryrd} %llbracket
21 \usepackage{bussproofs}
33 \usepackage{subcaption}
39 \usetikzlibrary{shapes,arrows,positioning}
40 % \usepackage{tikz-cd}
41 % \usepackage{pgfplots}
44 %% -----------------------------------------------------------------------------
46 \usepackage[english]{babel}
47 \usepackage[conor]{agda}
48 \renewcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathrm{\underline{#1}}}}
49 \renewcommand{\AgdaFunction}[1]{\textbf{\textcolor{AgdaFunction}{#1}}}
50 \renewcommand{\AgdaField}{\AgdaFunction}
51 % \definecolor{AgdaBound} {HTML}{000000}
52 \definecolor{AgdaHole} {HTML} {FFFF33}
54 \DeclareUnicodeCharacter{9665}{\ensuremath{\lhd}}
55 \DeclareUnicodeCharacter{964}{\ensuremath{\tau}}
56 \DeclareUnicodeCharacter{963}{\ensuremath{\sigma}}
57 \DeclareUnicodeCharacter{915}{\ensuremath{\Gamma}}
58 \DeclareUnicodeCharacter{8799}{\ensuremath{\stackrel{?}{=}}}
59 \DeclareUnicodeCharacter{9655}{\ensuremath{\rhd}}
61 \renewenvironment{code}%
62 {\noindent\ignorespaces\advance\leftskip\mathindent\AgdaCodeStyle\pboxed}%
63 {\endpboxed\par\noindent%
64 \ignorespacesafterend\small}
67 %% -----------------------------------------------------------------------------
70 \newcommand{\mysyn}{\AgdaKeyword}
71 \newcommand{\mytyc}{\AgdaDatatype}
72 \newcommand{\mydc}{\AgdaInductiveConstructor}
73 \newcommand{\myfld}{\AgdaField}
74 \newcommand{\myfun}{\AgdaFunction}
75 \newcommand{\myb}[1]{\AgdaBound{$#1$}}
76 \newcommand{\myfield}{\AgdaField}
77 \newcommand{\myind}{\AgdaIndent}
78 \newcommand{\mykant}{\textsc{Kant}}
79 \newcommand{\mysynel}[1]{#1}
80 \newcommand{\myse}{\mysynel}
81 \newcommand{\mytmsyn}{\mysynel{term}}
82 \newcommand{\mysp}{\ }
83 \newcommand{\myabs}[2]{\mydc{$\lambda$} #1 \mathrel{\mydc{$\mapsto$}} #2}
84 \newcommand{\myappsp}{\hspace{0.07cm}}
85 \newcommand{\myapp}[2]{#1 \myappsp #2}
86 \newcommand{\mysynsep}{\ \ |\ \ }
87 \newcommand{\myITE}[3]{\myfun{If}\, #1\, \myfun{Then}\, #2\, \myfun{Else}\, #3}
90 \newcommand{\mydesc}[3]{
96 \hfill \textbf{#1} $#2$
97 \framebox[\textwidth]{
111 \newcommand{\mytmt}{\mysynel{t}}
112 \newcommand{\mytmm}{\mysynel{m}}
113 \newcommand{\mytmn}{\mysynel{n}}
114 \newcommand{\myred}{\leadsto}
115 \newcommand{\mysub}[3]{#1[#2 / #3]}
116 \newcommand{\mytysyn}{\mysynel{type}}
117 \newcommand{\mybasetys}{K}
118 \newcommand{\mybasety}[1]{B_{#1}}
119 \newcommand{\mytya}{\myse{A}}
120 \newcommand{\mytyb}{\myse{B}}
121 \newcommand{\mytycc}{\myse{C}}
122 \newcommand{\myarr}{\mathrel{\textcolor{AgdaDatatype}{\to}}}
123 \newcommand{\myprod}{\mathrel{\textcolor{AgdaDatatype}{\times}}}
124 \newcommand{\myctx}{\Gamma}
125 \newcommand{\myvalid}[1]{#1 \vdash \underline{\mathrm{valid}}}
126 \newcommand{\myjudd}[3]{#1 \vdash #2 : #3}
127 \newcommand{\myjud}[2]{\myjudd{\myctx}{#1}{#2}}
128 \newcommand{\myabss}[3]{\mydc{$\lambda$} #1 {:} #2 \mathrel{\mydc{$\mapsto$}} #3}
129 \newcommand{\mytt}{\mydc{$\langle\rangle$}}
130 \newcommand{\myunit}{\mytyc{Unit}}
131 \newcommand{\mypair}[2]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #2\mathclose{\mydc{$\rangle$}}}
132 \newcommand{\myfst}{\myfld{fst}}
133 \newcommand{\mysnd}{\myfld{snd}}
134 \newcommand{\myconst}{\myse{c}}
135 \newcommand{\myemptyctx}{\cdot}
136 \newcommand{\myhole}{\AgdaHole}
137 \newcommand{\myfix}[3]{\mysyn{fix} \myappsp #1 {:} #2 \mapsto #3}
138 \newcommand{\mysum}{\mathbin{\textcolor{AgdaDatatype}{+}}}
139 \newcommand{\myleft}[1]{\mydc{left}_{#1}}
140 \newcommand{\myright}[1]{\mydc{right}_{#1}}
141 \newcommand{\myempty}{\mytyc{Empty}}
142 \newcommand{\mycase}[2]{\mathopen{\myfun{[}}#1\mathpunct{\myfun{,}} #2 \mathclose{\myfun{]}}}
143 \newcommand{\myabsurd}[1]{\myfun{absurd}_{#1}}
144 \newcommand{\myarg}{\_}
145 \newcommand{\myderivsp}{\vspace{0.3cm}}
146 \newcommand{\mytyp}{\mytyc{Type}}
147 \newcommand{\myneg}{\myfun{$\neg$}}
148 \newcommand{\myar}{\,}
149 \newcommand{\mybool}{\mytyc{Bool}}
150 \newcommand{\mytrue}{\mydc{true}}
151 \newcommand{\myfalse}{\mydc{false}}
152 \newcommand{\myitee}[5]{\myfun{if}\,#1 / {#2.#3}\,\myfun{then}\,#4\,\myfun{else}\,#5}
153 \newcommand{\mynat}{\mytyc{$\mathbb{N}$}}
154 \newcommand{\myrat}{\mytyc{$\mathbb{R}$}}
155 \newcommand{\myite}[3]{\myfun{if}\,#1\,\myfun{then}\,#2\,\myfun{else}\,#3}
156 \newcommand{\myfora}[3]{(#1 {:} #2) \myarr #3}
157 \newcommand{\myexi}[3]{(#1 {:} #2) \myprod #3}
158 \newcommand{\mypairr}[4]{\mathopen{\mydc{$\langle$}}#1\mathpunct{\mydc{,}} #4\mathclose{\mydc{$\rangle$}}_{#2{.}#3}}
159 \newcommand{\mylist}{\mytyc{List}}
160 \newcommand{\mynil}[1]{\mydc{[]}_{#1}}
161 \newcommand{\mycons}{\mathbin{\mydc{∷}}}
162 \newcommand{\myfoldr}{\myfun{foldr}}
163 \newcommand{\myw}[3]{\myapp{\myapp{\mytyc{W}}{(#1 {:} #2)}}{#3}}
164 \newcommand{\mynodee}{\mathbin{\mydc{$\lhd$}}}
165 \newcommand{\mynode}[2]{\mynodee_{#1.#2}}
166 \newcommand{\myrec}[4]{\myfun{rec}\,#1 / {#2.#3}\,\myfun{with}\,#4}
167 \newcommand{\mylub}{\sqcup}
168 \newcommand{\mydefeq}{\cong}
169 \newcommand{\myrefl}{\mydc{refl}}
170 \newcommand{\mypeq}[1]{\mathrel{\mytyc{=}_{#1}}}
171 \newcommand{\myjeqq}{\myfun{=-elim}}
172 \newcommand{\myjeq}[3]{\myapp{\myapp{\myapp{\myjeqq}{#1}}{#2}}{#3}}
173 \newcommand{\mysubst}{\myfun{subst}}
174 \newcommand{\myprsyn}{\myse{prop}}
175 \newcommand{\myprdec}[1]{\mathopen{\mytyc{$\llbracket$}} #1 \mathopen{\mytyc{$\rrbracket$}}}
176 \newcommand{\myand}{\mathrel{\mytyc{$\wedge$}}}
177 \newcommand{\myprfora}[3]{\forall #1 {:} #2. #3}
178 \newcommand{\myimpl}{\mathrel{\mytyc{$\Rightarrow$}}}
179 \newcommand{\mybot}{\mytyc{$\bot$}}
180 \newcommand{\mytop}{\mytyc{$\top$}}
181 \newcommand{\mycoe}{\myfun{coe}}
182 \newcommand{\mycoee}[4]{\myapp{\myapp{\myapp{\myapp{\mycoe}{#1}}{#2}}{#3}}{#4}}
183 \newcommand{\mycoh}{\myfun{coh}}
184 \newcommand{\mycohh}[4]{\myapp{\myapp{\myapp{\myapp{\mycoh}{#1}}{#2}}{#3}}{#4}}
185 \newcommand{\myjm}[4]{(#1 {:} #2) \mathrel{\mytyc{=}} (#3 {:} #4)}
186 \newcommand{\myeq}{\mathrel{\mytyc{=}}}
187 \newcommand{\myprop}{\mytyc{Prop}}
188 \newcommand{\mytmup}{\mytmsyn\uparrow}
189 \newcommand{\mydefs}{\Delta}
190 \newcommand{\mynf}{\Downarrow}
191 \newcommand{\myinff}[3]{#1 \vdash #2 \Rightarrow #3}
192 \newcommand{\myinf}[2]{\myinff{\myctx}{#1}{#2}}
193 \newcommand{\mychkk}[3]{#1 \vdash #2 \Leftarrow #3}
194 \newcommand{\mychk}[2]{\mychkk{\myctx}{#1}{#2}}
195 \newcommand{\myann}[2]{#1 : #2}
196 \newcommand{\mydeclsyn}{\myse{decl}}
197 \newcommand{\myval}[3]{#1 : #2 \mapsto #3}
198 \newcommand{\mypost}[2]{\mysyn{abstract}\ #1 : #2}
199 \newcommand{\myadt}[4]{\mysyn{data}\ #1 #2\ \mysyn{where}\ #3\{ #4 \}}
200 \newcommand{\myreco}[4]{\mysyn{record}\ #1 #2\ \mysyn{where}\ #3\ \{ #4 \}}
201 \newcommand{\myelabt}{\vdash}
202 \newcommand{\myelabf}{\rhd}
203 \newcommand{\myelab}[2]{\myctx \myelabt #1 \myelabf #2}
204 \newcommand{\mytele}{\Delta}
205 \newcommand{\mytelee}{\delta}
206 \newcommand{\mydcctx}{\Gamma}
207 \newcommand{\mynamesyn}{\myse{name}}
208 \newcommand{\myvec}{\overrightarrow}
209 \newcommand{\mymeta}{\textsc}
210 \newcommand{\myhyps}{\mymeta{hyps}}
211 \newcommand{\mycc}{;}
212 \newcommand{\myemptytele}{\cdot}
213 \newcommand{\mymetagoes}{\Longrightarrow}
214 % \newcommand{\mytesctx}{\
215 \newcommand{\mytelesyn}{\myse{telescope}}
216 \newcommand{\myrecs}{\mymeta{recs}}
217 \newcommand{\myle}{\mathrel{\lcfun{$\le$}}}
218 \newcommand{\mylet}{\mysyn{let}}
219 \newcommand{\myhead}{\mymeta{head}}
220 \newcommand{\mytake}{\mymeta{take}}
221 \newcommand{\myix}{\mymeta{ix}}
222 \newcommand{\myapply}{\mymeta{apply}}
223 \newcommand{\mydataty}{\mymeta{datatype}}
224 \newcommand{\myisreco}{\mymeta{record}}
225 \newcommand{\mydcsep}{\ |\ }
226 \newcommand{\mytree}{\mytyc{Tree}}
227 \newcommand{\myproj}[1]{\myfun{$\pi_{#1}$}}
230 %% -----------------------------------------------------------------------------
232 \title{\mykant: Implementing Observational Equality}
233 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
246 \begin{minipage}{0.5\textwidth}
247 \begin{flushleft} \large
249 Dr. Steffen \textsc{van Backel}
252 \begin{minipage}{0.5\textwidth}
253 \begin{flushright} \large
255 Dr. Philippa \textsc{Gardner}
262 The marriage between programming and logic has been a very fertile one. In
263 particular, since the simply typed lambda calculus (STLC), a number of type
264 systems have been devised with increasing expressive power.
266 Among this systems, Inutitionistic Type Theory (ITT) has been a very
267 popular framework for theorem provers and programming languages.
268 However, equality has always been a tricky business in ITT and related
271 In these thesis we will explain why this is the case, and present
272 Observational Type Theory (OTT), a solution to some of the problems
273 with equality. We then describe $\mykant$, a theorem prover featuring
274 OTT in a setting more close to the one found in current systems.
275 Having implemented part of $\mykant$ as a Haskell program, we describe
276 some of the implementation issues faced.
281 \renewcommand{\abstractname}{Acknowledgements}
283 I would like to thank Steffen van Backel, my supervisor, who was brave
284 enough to believe in my project and who provided much advice and
287 I would also like to thank the Haskell and Agda community on
288 \texttt{IRC}, which guided me through the strange world of types; and
289 in particular Andrea Vezzosi and James Deikun, with whom I entertained
290 countless insightful discussions in the past year. Andrea suggested
291 Observational Type Theory as a topic of study: this thesis would not
294 Finally, much of the work stems from the research of Conor McBride,
295 who answered many of my doubts through these months. I also owe him
305 \section{Simple and not-so-simple types}
308 \subsection{The untyped $\lambda$-calculus}
310 Along with Turing's machines, the earliest attempts to formalise computation
311 lead to the $\lambda$-calculus \citep{Church1936}. This early programming
312 language encodes computation with a minimal syntax and no `data' in the
313 traditional sense, but just functions. Here we give a brief overview of the
314 language, which will give the chance to introduce concepts central to the
315 analysis of all the following calculi. The exposition follows the one found in
316 chapter 5 of \cite{Queinnec2003}.
318 The syntax of $\lambda$-terms consists of three things: variables, abstractions,
323 \begin{array}{r@{\ }c@{\ }l}
324 \mytmsyn & ::= & \myb{x} \mysynsep \myabs{\myb{x}}{\mytmsyn} \mysynsep (\myapp{\mytmsyn}{\mytmsyn}) \\
325 x & \in & \text{Some enumerable set of symbols}
330 Parenthesis will be omitted in the usual way:
331 $\myapp{\myapp{\mytmt}{\mytmm}}{\mytmn} =
332 \myapp{(\myapp{\mytmt}{\mytmm})}{\mytmn}$.
334 Abstractions roughly corresponds to functions, and their semantics is more
335 formally explained by the $\beta$-reduction rule:
337 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
340 \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}\text{, where} \\
342 \begin{array}{l@{\ }c@{\ }l}
343 \mysub{\myb{x}}{\myb{x}}{\mytmn} & = & \mytmn \\
344 \mysub{\myb{y}}{\myb{x}}{\mytmn} & = & y\text{, with } \myb{x} \neq y \\
345 \mysub{(\myapp{\mytmt}{\mytmm})}{\myb{x}}{\mytmn} & = & (\myapp{\mysub{\mytmt}{\myb{x}}{\mytmn}}{\mysub{\mytmm}{\myb{x}}{\mytmn}}) \\
346 \mysub{(\myabs{\myb{x}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{x}}{\mytmm} \\
347 \mysub{(\myabs{\myb{y}}{\mytmm})}{\myb{x}}{\mytmn} & = & \myabs{\myb{z}}{\mysub{\mysub{\mytmm}{\myb{y}}{\myb{z}}}{\myb{x}}{\mytmn}}, \\
348 \multicolumn{3}{l}{\myind{1} \text{with $\myb{x} \neq \myb{y}$ and $\myb{z}$ not free in $\myapp{\mytmm}{\mytmn}$}}
354 The care required during substituting variables for terms is required to avoid
355 name capturing. We will use substitution in the future for other name-binding
356 constructs assuming similar precautions.
358 These few elements are of remarkable expressiveness, and in fact Turing
359 complete. As a corollary, we must be able to devise a term that reduces forever
360 (`loops' in imperative terms):
363 (\myapp{\omega}{\omega}) \myred (\myapp{\omega}{\omega}) \myred \cdots \text{, with $\omega = \myabs{x}{\myapp{x}{x}}$}
367 A \emph{redex} is a term that can be reduced. In the untyped $\lambda$-calculus
368 this will be the case for an application in which the first term is an
369 abstraction, but in general we call aterm reducible if it appears to the left of
370 a reduction rule. When a term contains no redexes it's said to be in
371 \emph{normal form}. Given the observation above, not all terms reduce to a
372 normal forms: we call the ones that do \emph{normalising}, and the ones that
373 don't \emph{non-normalising}.
375 The reduction rule presented is not syntax directed, but \emph{evaluation
376 strategies} can be employed to reduce term systematically. Common evaluation
377 strategies include \emph{call by value} (or \emph{strict}), where arguments of
378 abstractions are reduced before being applied to the abstraction; and conversely
379 \emph{call by name} (or \emph{lazy}), where we reduce only when we need to do so
380 to proceed---in other words when we have an application where the function is
381 still not a $\lambda$. In both these reduction strategies we never reduce under
382 an abstraction: for this reason a weaker form of normalisation is used, where
383 both abstractions and normal forms are said to be in \emph{weak head normal
386 \subsection{The simply typed $\lambda$-calculus}
388 A convenient way to `discipline' and reason about $\lambda$-terms is to assign
389 \emph{types} to them, and then check that the terms that we are forming make
390 sense given our typing rules \citep{Curry1934}. The first most basic instance
391 of this idea takes the name of \emph{simply typed $\lambda$ calculus}, whose
392 rules are shown in figure \ref{fig:stlc}.
394 Our types contain a set of \emph{type variables} $\Phi$, which might
395 correspond to some `primitive' types; and $\myarr$, the type former for
396 `arrow' types, the types of functions. The language is explicitly
397 typed: when we bring a variable into scope with an abstraction, we
398 declare its type. Reduction is unchanged from the untyped
404 \begin{array}{r@{\ }c@{\ }l}
405 \mytmsyn & ::= & \myb{x} \mysynsep \myabss{\myb{x}}{\mytysyn}{\mytmsyn} \mysynsep
406 (\myapp{\mytmsyn}{\mytmsyn}) \\
407 \mytysyn & ::= & \myse{\phi} \mysynsep \mytysyn \myarr \mytysyn \mysynsep \\
408 \myb{x} & \in & \text{Some enumerable set of symbols} \\
409 \myse{\phi} & \in & \Phi
414 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
416 \AxiomC{$\myctx(x) = A$}
417 \UnaryInfC{$\myjud{\myb{x}}{A}$}
420 \AxiomC{$\myjudd{\myctx;\myb{x} : A}{\mytmt}{\mytyb}$}
421 \UnaryInfC{$\myjud{\myabss{x}{A}{\mytmt}}{\mytyb}$}
424 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
425 \AxiomC{$\myjud{\mytmn}{\mytya}$}
426 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mytyb}$}
430 \caption{Syntax and typing rules for the STLC. Reduction is unchanged from
431 the untyped $\lambda$-calculus.}
435 In the typing rules, a context $\myctx$ is used to store the types of bound
436 variables: $\myctx; \myb{x} : \mytya$ adds a variable to the context and
437 $\myctx(x)$ returns the type of the rightmost occurrence of $x$.
439 This typing system takes the name of `simply typed lambda calculus' (STLC), and
440 enjoys a number of properties. Two of them are expected in most type systems
443 \item[Progress] A well-typed term is not stuck---it is either a variable, or its
444 constructor does not appear on the left of the $\myred$ relation (currently
445 only $\lambda$), or it can take a step according to the evaluation rules.
446 \item[Preservation] If a well-typed term takes a step of evaluation, then the
447 resulting term is also well-typed, and preserves the previous type. Also
448 known as \emph{subject reduction}.
451 However, STLC buys us much more: every well-typed term is normalising
452 \citep{Tait1967}. It is easy to see that we can't fill the blanks if we want to
453 give types to the non-normalising term shown before:
455 \myapp{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}{(\myabss{\myb{x}}{\myhole{?}}{\myapp{\myb{x}}{\myb{x}}})}
458 This makes the STLC Turing incomplete. We can recover the ability to loop by
459 adding a combinator that recurses:
462 \begin{minipage}{0.5\textwidth}
464 $ \mytmsyn ::= \cdots b \mysynsep \myfix{\myb{x}}{\mytysyn}{\mytmsyn} $
468 \begin{minipage}{0.5\textwidth}
469 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}} {
470 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytya}$}
471 \UnaryInfC{$\myjud{\myfix{\myb{x}}{\mytya}{\mytmt}}{\mytya}$}
476 \mydesc{reduction:}{\myjud{\mytmsyn}{\mytmsyn}}{
477 $ \myfix{\myb{x}}{\mytya}{\mytmt} \myred \mysub{\mytmt}{\myb{x}}{(\myfix{\myb{x}}{\mytya}{\mytmt})}$
480 This will deprive us of normalisation, which is a particularly bad thing if we
481 want to use the STLC as described in the next section.
483 \subsection{The Curry-Howard correspondence}
485 It turns out that the STLC can be seen a natural deduction system for
486 intuitionistic propositional logic. Terms are proofs, and their types are the
487 propositions they prove. This remarkable fact is known as the Curry-Howard
488 correspondence, or isomorphism.
490 The arrow ($\myarr$) type corresponds to implication. If we wish to prove that
491 that $(\mytya \myarr \mytyb) \myarr (\mytyb \myarr \mytycc) \myarr (\mytya
492 \myarr \mytycc)$, all we need to do is to devise a $\lambda$-term that has the
495 \myabss{\myb{f}}{(\mytya \myarr \mytyb)}{\myabss{\myb{g}}{(\mytyb \myarr \mytycc)}{\myabss{\myb{x}}{\mytya}{\myapp{\myb{g}}{(\myapp{\myb{f}}{\myb{x}})}}}}
497 That is, function composition. Going beyond arrow types, we can extend our bare
498 lambda calculus with useful types to represent other logical constructs, as
499 shown in figure \ref{fig:natded}.
504 \begin{array}{r@{\ }c@{\ }l}
505 \mytmsyn & ::= & \cdots \\
506 & | & \mytt \mysynsep \myapp{\myabsurd{\mytysyn}}{\mytmsyn} \\
507 & | & \myapp{\myleft{\mytysyn}}{\mytmsyn} \mysynsep
508 \myapp{\myright{\mytysyn}}{\mytmsyn} \mysynsep
509 \myapp{\mycase{\mytmsyn}{\mytmsyn}}{\mytmsyn} \\
510 & | & \mypair{\mytmsyn}{\mytmsyn} \mysynsep
511 \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
512 \mytysyn & ::= & \cdots \mysynsep \myunit \mysynsep \myempty \mysynsep \mytmsyn \mysum \mytmsyn \mysynsep \mytysyn \myprod \mytysyn
517 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
520 \begin{array}{l@{ }l@{\ }c@{\ }l}
521 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myleft{\mytya} &}{\mytmt})} & \myred &
522 \myapp{\mytmm}{\mytmt} \\
523 \myapp{\mycase{\mytmm}{\mytmn}}{(\myapp{\myright{\mytya} &}{\mytmt})} & \myred &
524 \myapp{\mytmn}{\mytmt}
529 \begin{array}{l@{ }l@{\ }c@{\ }l}
530 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
531 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
537 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
539 \AxiomC{\phantom{$\myjud{\mytmt}{\myempty}$}}
540 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
543 \AxiomC{$\myjud{\mytmt}{\myempty}$}
544 \UnaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
551 \AxiomC{$\myjud{\mytmt}{\mytya}$}
552 \UnaryInfC{$\myjud{\myapp{\myleft{\mytyb}}{\mytmt}}{\mytya \mysum \mytyb}$}
555 \AxiomC{$\myjud{\mytmt}{\mytyb}$}
556 \UnaryInfC{$\myjud{\myapp{\myright{\mytya}}{\mytmt}}{\mytya \mysum \mytyb}$}
564 \AxiomC{$\myjud{\mytmm}{\mytya \myarr \mytyb}$}
565 \AxiomC{$\myjud{\mytmn}{\mytya \myarr \mytycc}$}
566 \AxiomC{$\myjud{\mytmt}{\mytya \mysum \mytyb}$}
567 \TrinaryInfC{$\myjud{\myapp{\mycase{\mytmm}{\mytmn}}{\mytmt}}{\mytycc}$}
574 \AxiomC{$\myjud{\mytmm}{\mytya}$}
575 \AxiomC{$\myjud{\mytmn}{\mytyb}$}
576 \BinaryInfC{$\myjud{\mypair{\mytmm}{\mytmn}}{\mytya \myprod \mytyb}$}
579 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
580 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
583 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
584 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
588 \caption{Rules for the extendend STLC. Only the new features are shown, all the
589 rules and syntax for the STLC apply here too.}
593 Tagged unions (or sums, or coproducts---$\mysum$ here, \texttt{Either}
594 in Haskell) correspond to disjunctions, and dually tuples (or pairs, or
595 products---$\myprod$ here, tuples in Haskell) correspond to
596 conjunctions. This is apparent looking at the ways to construct and
597 destruct the values inhabiting those types: for $\mysum$ $\myleft{ }$
598 and $\myright{ }$ correspond to $\vee$ introduction, and
599 $\mycase{\myarg}{\myarg}$ to $\vee$ elimination; for $\myprod$
600 $\mypair{\myarg}{\myarg}$ corresponds to $\wedge$ introduction, $\myfst$
601 and $\mysnd$ to $\wedge$ elimination.
603 The trivial type $\myunit$ corresponds to the logical $\top$, and dually
604 $\myempty$ corresponds to the logical $\bot$. $\myunit$ has one introduction
605 rule ($\mytt$), and thus one inhabitant; and no eliminators. $\myempty$ has no
606 introduction rules, and thus no inhabitants; and one eliminator ($\myabsurd{
607 }$), corresponding to the logical \emph{ex falso quodlibet}.
609 With these rules, our STLC now looks remarkably similar in power and use to the
610 natural deduction we already know. $\myneg \mytya$ can be expressed as $\mytya
611 \myarr \myempty$. However, there is an important omission: there is no term of
612 the type $\mytya \mysum \myneg \mytya$ (excluded middle), or equivalently
613 $\myneg \myneg \mytya \myarr \mytya$ (double negation), or indeed any term with
614 a type equivalent to those.
616 This has a considerable effect on our logic and it's no coincidence, since there
617 is no obvious computational behaviour for laws like the excluded middle.
618 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
619 and all the systems analysed will have this characteristic since they build on
620 the foundation of the STLC\footnote{There is research to give computational
621 behaviour to classical logic, but I will not touch those subjects.}.
623 As in logic, if we want to keep our system consistent, we must make sure that no
624 closed terms (in other words terms not under a $\lambda$) inhabit $\myempty$.
625 The variant of STLC presented here is indeed
626 consistent, a result that follows from the fact that it is
627 normalising. % TODO explain
628 Going back to our $\mysyn{fix}$ combinator, it is easy to see how it ruins our
629 desire for consistency. The following term works for every type $\mytya$,
632 (\myfix{\myb{x}}{\mytya}{\myb{x}}) : \mytya
635 \subsection{Inductive data}
638 To make the STLC more useful as a programming language or reasoning tool it is
639 common to include (or let the user define) inductive data types. These comprise
640 of a type former, various constructors, and an eliminator (or destructor) that
641 serves as primitive recursor.
643 For example, we might add a $\mylist$ type constructor, along with an `empty
644 list' ($\mynil{ }$) and `cons cell' ($\mycons$) constructor. The eliminator for
645 lists will be the usual folding operation ($\myfoldr$). See figure
651 \begin{array}{r@{\ }c@{\ }l}
652 \mytmsyn & ::= & \cdots \mysynsep \mynil{\mytysyn} \mysynsep \mytmsyn \mycons \mytmsyn
654 \myapp{\myapp{\myapp{\myfoldr}{\mytmsyn}}{\mytmsyn}}{\mytmsyn} \\
655 \mytysyn & ::= & \cdots \mysynsep \myapp{\mylist}{\mytysyn}
659 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
661 \begin{array}{l@{\ }c@{\ }l}
662 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mynil{\mytya}} & \myred & \mytmt \\
664 \myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{(\mytmm \mycons \mytmn)} & \myred &
665 \myapp{\myapp{\myse{f}}{\mytmm}}{(\myapp{\myapp{\myapp{\myfoldr}{\myse{f}}}{\mytmt}}{\mytmn})}
669 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
671 \AxiomC{\phantom{$\myjud{\mytmm}{\mytya}$}}
672 \UnaryInfC{$\myjud{\mynil{\mytya}}{\myapp{\mylist}{\mytya}}$}
675 \AxiomC{$\myjud{\mytmm}{\mytya}$}
676 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
677 \BinaryInfC{$\myjud{\mytmm \mycons \mytmn}{\myapp{\mylist}{\mytya}}$}
682 \AxiomC{$\myjud{\mysynel{f}}{\mytya \myarr \mytyb \myarr \mytyb}$}
683 \AxiomC{$\myjud{\mytmm}{\mytyb}$}
684 \AxiomC{$\myjud{\mytmn}{\myapp{\mylist}{\mytya}}$}
685 \TrinaryInfC{$\myjud{\myapp{\myapp{\myapp{\myfoldr}{\mysynel{f}}}{\mytmm}}{\mytmn}}{\mytyb}$}
688 \caption{Rules for lists in the STLC.}
692 In section \ref{sec:well-order} we will see how to give a general account of
693 inductive data. %TODO does this make sense to have here?
695 \section{Intuitionistic Type Theory}
698 \subsection{Extending the STLC}
700 The STLC can be made more expressive in various ways. \cite{Barendregt1991}
701 succinctly expressed geometrically how we can add expressivity:
705 & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
706 & & \lambda C \ar@{-}[dd]
708 \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
709 & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
711 & \lambda\underline\omega \ar@{-}'[r][rr]
712 & & \lambda P\underline\omega
714 \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
715 & & \lambda P \ar@{-}[ur]
718 Here $\lambda{\to}$, in the bottom left, is the STLC. From there can move along
721 \item[Terms depending on types (towards $\lambda{2}$)] We can quantify over
722 types in our type signatures. For example, we can define a polymorphic
724 {\small\[\displaystyle
725 (\myabss{\myb{A}}{\mytyp}{\myabss{\myb{x}}{\myb{A}}{\myb{x}}}) : (\myb{A} : \mytyp) \myarr \myb{A} \myarr \myb{A}
727 The first and most famous instance of this idea has been System F. This form
728 of polymorphism and has been wildly successful, also thanks to a well known
729 inference algorithm for a restricted version of System F known as
730 Hindley-Milner. Languages like Haskell and SML are based on this discipline.
731 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] We have
732 type operators. For example we could define a function that given types $R$
733 and $\mytya$ forms the type that represents a value of type $\mytya$ in
734 continuation passing style: {\small\[\displaystyle(\myabss{\myb{A} \myar \myb{R}}{\mytyp}{(\myb{A}
735 \myarr \myb{R}) \myarr \myb{R}}) : \mytyp \myarr \mytyp \myarr \mytyp\]}
736 \item[Types depending on terms (towards $\lambda{P}$)] Also known as `dependent
737 types', give great expressive power. For example, we can have values of whose
738 type depend on a boolean:
739 {\small\[\displaystyle(\myabss{\myb{x}}{\mybool}{\myite{\myb{x}}{\mynat}{\myrat}}) : \mybool
743 All the systems preserve the properties that make the STLC well behaved. The
744 system we are going to focus on, Intuitionistic Type Theory, has all of the
745 above additions, and thus would sit where $\lambda{C}$ sits in the
746 `$\lambda$-cube'. It will serve as the logical `core' of all the other
747 extensions that we will present and ultimately our implementation of a similar
750 \subsection{A Bit of History}
752 Logic frameworks and programming languages based on type theory have a long
753 history. Per Martin-L\"{o}f described the first version of his theory in 1971,
754 but then revised it since the original version was inconsistent due to its
755 impredicativity\footnote{In the early version there was only one universe
756 $\mytyp$ and $\mytyp : \mytyp$, see section \ref{sec:term-types} for an
757 explanation on why this causes problems.}. For this reason he gave a revised
758 and consistent definition later \citep{Martin-Lof1984}.
760 A related development is the polymorphic $\lambda$-calculus, and specifically
761 the previously mentioned System F, which was developed independently by Girard
762 and Reynolds. An overview can be found in \citep{Reynolds1994}. The surprising
763 fact is that while System F is impredicative it is still consistent and strongly
764 normalising. \cite{Coquand1986} further extended this line of work with the
765 Calculus of Constructions (CoC).
767 Most widely used interactive theorem provers are based on ITT. Popular ones
768 include Agda \citep{Norell2007, Bove2009}, Coq \citep{Coq}, and Epigram
769 \citep{McBride2004, EpigramTut}.
771 \subsection{A note on inference}
773 % TODO do this, adding links to the sections about bidi type checking and
774 % implicit universes.
775 In the following text I will often omit explicit typing for abstractions or
777 Moreover, I will use $\mytyp$ without bothering to specify a
778 universe, with the silent assumption that the definition is consistent
779 regarding to the hierarchy.
781 \subsection{A simple type theory}
784 The calculus I present follows the exposition in \citep{Thompson1991},
785 and is quite close to the original formulation of predicative ITT as
786 found in \citep{Martin-Lof1984}. The system's syntax and reduction
787 rules are presented in their entirety in figure \ref{fig:core-tt-syn}.
788 The typing rules are presented piece by piece. Agda and \mykant\
789 renditions of the presented theory and all the examples is reproduced in
790 appendix \ref{app:itt-code}.
795 \begin{array}{r@{\ }c@{\ }l}
796 \mytmsyn & ::= & \myb{x} \mysynsep
798 \myunit \mysynsep \mytt \mysynsep
799 \myempty \mysynsep \myapp{\myabsurd{\mytmsyn}}{\mytmsyn} \\
800 & | & \mybool \mysynsep \mytrue \mysynsep \myfalse \mysynsep
801 \myitee{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
802 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
803 \myabss{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
804 (\myapp{\mytmsyn}{\mytmsyn}) \\
805 & | & \myexi{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
806 \mypairr{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
807 & | & \myapp{\myfst}{\mytmsyn} \mysynsep \myapp{\mysnd}{\mytmsyn} \\
808 & | & \myw{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
809 \mytmsyn \mynode{\myb{x}}{\mytmsyn} \mytmsyn \\
810 & | & \myrec{\mytmsyn}{\myb{x}}{\mytmsyn}{\mytmsyn} \\
816 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
819 \begin{array}{l@{ }l@{\ }c@{\ }l}
820 \myitee{\mytrue &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmm \\
821 \myitee{\myfalse &}{\myb{x}}{\myse{P}}{\mytmm}{\mytmn} & \myred & \mytmn \\
826 \myapp{(\myabss{\myb{x}}{\mytya}{\mytmm})}{\mytmn} \myred \mysub{\mytmm}{\myb{x}}{\mytmn}
830 \begin{array}{l@{ }l@{\ }c@{\ }l}
831 \myapp{\myfst &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmm \\
832 \myapp{\mysnd &}{\mypair{\mytmm}{\mytmn}} & \myred & \mytmn
840 \myrec{(\myse{s} \mynode{\myb{x}}{\myse{T}} \myse{f})}{\myb{y}}{\myse{P}}{\myse{p}} \myred
841 \myapp{\myapp{\myapp{\myse{p}}{\myse{s}}}{\myse{f}}}{(\myabss{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}}}{
842 \myrec{\myapp{\myse{f}}{\myb{t}}}{\myb{y}}{\myse{P}}{\mytmt}
846 \caption{Syntax and reduction rules for our type theory.}
847 \label{fig:core-tt-syn}
850 \subsubsection{Types are terms, some terms are types}
851 \label{sec:term-types}
853 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
855 \AxiomC{$\myjud{\mytmt}{\mytya}$}
856 \AxiomC{$\mytya \mydefeq \mytyb$}
857 \BinaryInfC{$\myjud{\mytmt}{\mytyb}$}
860 \AxiomC{\phantom{$\myjud{\mytmt}{\mytya}$}}
861 \UnaryInfC{$\myjud{\mytyp_l}{\mytyp_{l + 1}}$}
866 The first thing to notice is that a barrier between values and types that we had
867 in the STLC is gone: values can appear in types, and the two are treated
868 uniformly in the syntax.
870 While the usefulness of doing this will become clear soon, a consequence is
871 that since types can be the result of computation, deciding type equality is
872 not immediate as in the STLC. For this reason we define \emph{definitional
873 equality}, $\mydefeq$, as the congruence relation extending
874 $\myred$---moreover, when comparing types syntactically we do it up to
875 renaming of bound names ($\alpha$-renaming). For example under this
876 discipline we will find that
878 \myabss{\myb{x}}{\mytya}{\myb{x}} \mydefeq \myabss{\myb{y}}{\mytya}{\myb{y}}
880 Types that are definitionally equal can be used interchangeably. Here
881 the `conversion' rule is not syntax directed, but it is possible to
882 employ $\myred$ to decide term equality in a systematic way, by always
883 reducing terms to their normal forms before comparing them, so that a
884 separate conversion rule is not needed. % TODO add section
885 Another thing to notice is that considering the need to reduce terms to
886 decide equality, it is essential for a dependently type system to be
887 terminating and confluent for type checking to be decidable.
889 Moreover, we specify a \emph{type hierarchy} to talk about `large'
890 types: $\mytyp_0$ will be the type of types inhabited by data:
891 $\mybool$, $\mynat$, $\mylist$, etc. $\mytyp_1$ will be the type of
892 $\mytyp_0$, and so on---for example we have $\mytrue : \mybool :
893 \mytyp_0 : \mytyp_1 : \cdots$. Each type `level' is often called a
894 universe in the literature. While it is possible to simplify things by
895 having only one universe $\mytyp$ with $\mytyp : \mytyp$, this plan is
896 inconsistent for much the same reason that impredicative na\"{\i}ve set
897 theory is \citep{Hurkens1995}. However various techniques can be
898 employed to lift the burden of explicitly handling universes, as we will
899 see in section \ref{sec:term-hierarchy}.
901 \subsubsection{Contexts}
903 \begin{minipage}{0.5\textwidth}
904 \mydesc{context validity:}{\myvalid{\myctx}}{
906 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
907 \UnaryInfC{$\myvalid{\myemptyctx}$}
910 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
911 \UnaryInfC{$\myvalid{\myctx ; \myb{x} : \mytya}$}
916 \begin{minipage}{0.5\textwidth}
917 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
918 \AxiomC{$\myctx(x) = \mytya$}
919 \UnaryInfC{$\myjud{\myb{x}}{\mytya}$}
925 We need to refine the notion context to make sure that every variable appearing
926 is typed correctly, or that in other words each type appearing in the context is
927 indeed a type and not a value. In every other rule, if no premises are present,
928 we assume the context in the conclusion to be valid.
930 Then we can re-introduce the old rule to get the type of a variable for a
933 \subsubsection{$\myunit$, $\myempty$}
935 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
937 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
938 \UnaryInfC{$\myjud{\myunit}{\mytyp_0}$}
940 \UnaryInfC{$\myjud{\myempty}{\mytyp_0}$}
943 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
944 \UnaryInfC{$\myjud{\mytt}{\myunit}$}
946 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
949 \AxiomC{$\myjud{\mytmt}{\myempty}$}
950 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
951 \BinaryInfC{$\myjud{\myapp{\myabsurd{\mytya}}{\mytmt}}{\mytya}$}
953 \UnaryInfC{\phantom{$\myjud{\myempty}{\mytyp_0}$}}
958 Nothing surprising here: $\myunit$ and $\myempty$ are unchanged from the STLC,
959 with the added rules to type $\myunit$ and $\myempty$ themselves, and to make
960 sure that we are invoking $\myabsurd{}$ over a type.
962 \subsubsection{$\mybool$, and dependent $\myfun{if}$}
964 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
967 \UnaryInfC{$\myjud{\mybool}{\mytyp_0}$}
971 \UnaryInfC{$\myjud{\mytrue}{\mybool}$}
975 \UnaryInfC{$\myjud{\myfalse}{\mybool}$}
980 \AxiomC{$\myjud{\mytmt}{\mybool}$}
981 \AxiomC{$\myjudd{\myctx : \mybool}{\mytya}{\mytyp_l}$}
983 \BinaryInfC{$\myjud{\mytmm}{\mysub{\mytya}{x}{\mytrue}}$ \hspace{0.7cm} $\myjud{\mytmn}{\mysub{\mytya}{x}{\myfalse}}$}
984 \UnaryInfC{$\myjud{\myitee{\mytmt}{\myb{x}}{\mytya}{\mytmm}{\mytmn}}{\mysub{\mytya}{\myb{x}}{\mytmt}}$}
988 With booleans we get the first taste of the `dependent' in `dependent
989 types'. While the two introduction rules ($\mytrue$ and $\myfalse$) are
990 not surprising, the typing rules for $\myfun{if}$ are. In most strongly
991 typed languages we expect the branches of an $\myfun{if}$ statements to
992 be of the same type, to preserve subject reduction, since execution
993 could take both paths. This is a pity, since the type system does not
994 reflect the fact that in each branch we gain knowledge on the term we
995 are branching on. Which means that programs along the lines of
996 {\small\[\text{\texttt{if null xs then head xs else 0}}\]}
997 are a necessary, well typed, danger.
999 However, in a more expressive system, we can do better: the branches' type can
1000 depend on the value of the scrutinised boolean. This is what the typing rule
1001 expresses: the user provides a type $\mytya$ ranging over an $\myb{x}$
1002 representing the scrutinised boolean type, and the branches are typechecked with
1003 the updated knowledge on the value of $\myb{x}$.
1005 \subsubsection{$\myarr$, or dependent function}
1007 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1008 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1009 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1010 \BinaryInfC{$\myjud{\myfora{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1016 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytmt}{\mytyb}$}
1017 \UnaryInfC{$\myjud{\myabss{\myb{x}}{\mytya}{\mytmt}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1020 \AxiomC{$\myjud{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1021 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1022 \BinaryInfC{$\myjud{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1027 Dependent functions are one of the two key features that perhaps most
1028 characterise dependent types---the other being dependent products. With
1029 dependent functions, the result type can depend on the value of the
1030 argument. This feature, together with the fact that the result type
1031 might be a type itself, brings a lot of interesting possibilities.
1032 Following this intuition, in the introduction rule, the return type is
1033 typechecked in a context with an abstracted variable of lhs' type, and
1034 in the elimination rule the actual argument is substituted in the return
1035 type. Keeping the correspondence with logic alive, dependent functions
1036 are much like universal quantifiers ($\forall$) in logic.
1038 For example, assuming that we have lists and natural numbers in our
1039 language, using dependent functions we would be able to
1043 \myfun{length} : (\myb{A} {:} \mytyp_0) \myarr \myapp{\mylist}{\myb{A}} \myarr \mynat \\
1044 \myarg \myfun{$>$} \myarg : \mynat \myarr \mynat \myarr \mytyp_0 \\
1045 \myfun{head} : (\myb{A} {:} \mytyp_0) \myarr (\myb{l} {:} \myapp{\mylist}{\myb{A}})
1046 \myarr \myapp{\myapp{\myfun{length}}{\myb{A}}}{\myb{l}} \mathrel{\myfun{>}} 0 \myarr
1051 \myfun{length} is the usual polymorphic length function. $\myfun{>}$ is
1052 a function that takes two naturals and returns a type: if the lhs is
1053 greater then the rhs, $\myunit$ is returned, $\myempty$ otherwise. This
1054 way, we can express a `non-emptyness' condition in $\myfun{head}$, by
1055 including a proof that the length of the list argument is non-zero.
1056 This allows us to rule out the `empty list' case, so that we can safely
1057 return the first element.
1059 Again, we need to make sure that the type hierarchy is respected, which is the
1060 reason why a type formed by $\myarr$ will live in the least upper bound of the
1061 levels of argument and return type. This trend will continue with the other
1062 type-level binders, $\myprod$ and $\mytyc{W}$.
1064 \subsubsection{$\myprod$, or dependent product}
1067 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1068 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1069 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1070 \BinaryInfC{$\myjud{\myexi{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1076 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1077 \AxiomC{$\myjud{\mytmn}{\mysub{\mytyb}{\myb{x}}{\mytmm}}$}
1078 \BinaryInfC{$\myjud{\mypairr{\mytmm}{\myb{x}}{\mytyb}{\mytmn}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1080 \UnaryInfC{\phantom{$--$}}
1083 \AxiomC{$\myjud{\mytmt}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1084 \UnaryInfC{$\hspace{0.7cm}\myjud{\myapp{\myfst}{\mytmt}}{\mytya}\hspace{0.7cm}$}
1086 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mysub{\mytyb}{\myb{x}}{\myapp{\myfst}{\mytmt}}}$}
1091 If dependent functions are a generalisation of $\myarr$ in the STLC,
1092 dependent products are a generalisation of $\myprod$ in the STLC. The
1093 improvement is that the second element's type can depend on the value of
1094 the first element. The corrispondence with logic is through the
1095 existential quantifier: $\exists x \in \mathbb{N}. even(x)$ can be
1096 expressed as $\myexi{\myb{x}}{\mynat}{\myapp{\myfun{even}}{\myb{x}}}$.
1097 The first element will be a number, and the second evidence that the
1098 number is even. This highlights the fact that we are working in a
1099 constructive logic: if we have an existence proof, we can always ask for
1100 a witness. This means, for instance, that $\neg \forall \neg$ is not
1101 equivalent to $\exists$.
1103 Another perhaps more `dependent' application of products, paired with
1104 $\mybool$, is to offer choice between different types. For example we
1105 can easily recover disjunctions:
1108 \myarg\myfun{$\vee$}\myarg : \mytyp_0 \myarr \mytyp_0 \myarr \mytyp_0 \\
1109 \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \mapsto \myexi{\myb{x}}{\mybool}{\myite{\myb{x}}{\myb{A}}{\myb{B}}} \\ \ \\
1110 \myfun{case} : (\myb{A}\ \myb{B}\ \myb{C} {:} \mytyp_0) \myarr (\myb{A} \myarr \myb{C}) \myarr (\myb{B} \myarr \myb{C}) \myarr \myb{A} \mathrel{\myfun{$\vee$}} \myb{B} \myarr \myb{C} \\
1111 \myfun{case} \myappsp \myb{A} \myappsp \myb{B} \myappsp \myb{C} \myappsp \myb{f} \myappsp \myb{g} \myappsp \myb{x} \mapsto \\
1112 \myind{2} \myapp{(\myitee{\myapp{\myfst}{\myb{b}}}{\myb{x}}{(\myite{\myb{b}}{\myb{A}}{\myb{B}})}{\myb{f}}{\myb{g}})}{(\myapp{\mysnd}{\myb{x}})}
1116 \subsubsection{$\mytyc{W}$, or well-order}
1117 \label{sec:well-order}
1119 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1120 \AxiomC{$\myjud{\mytya}{\mytyp_{l_1}}$}
1121 \AxiomC{$\myjudd{\myctx;\myb{x} : \mytya}{\mytyb}{\mytyp_{l_2}}$}
1122 \BinaryInfC{$\myjud{\myw{\myb{x}}{\mytya}{\mytyb}}{\mytyp_{l_1 \mylub l_2}}$}
1127 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1128 \AxiomC{$\myjud{\mysynel{f}}{\mysub{\mytyb}{\myb{x}}{\mytmt} \myarr \myw{\myb{x}}{\mytya}{\mytyb}}$}
1129 \BinaryInfC{$\myjud{\mytmt \mynode{\myb{x}}{\mytyb} \myse{f}}{\myw{\myb{x}}{\mytya}{\mytyb}}$}
1134 \AxiomC{$\myjud{\myse{u}}{\myw{\myb{x}}{\myse{S}}{\myse{T}}}$}
1135 \AxiomC{$\myjudd{\myctx; \myb{w} : \myw{\myb{x}}{\myse{S}}{\myse{T}}}{\myse{P}}{\mytyp_l}$}
1137 \BinaryInfC{$\myjud{\myse{p}}{
1138 \myfora{\myb{s}}{\myse{S}}{\myfora{\myb{f}}{\mysub{\myse{T}}{\myb{x}}{\myse{s}} \myarr \myw{\myb{x}}{\myse{S}}{\myse{T}}}{(\myfora{\myb{t}}{\mysub{\myse{T}}{\myb{x}}{\myb{s}}}{\mysub{\myse{P}}{\myb{w}}{\myapp{\myb{f}}{\myb{t}}}}) \myarr \mysub{\myse{P}}{\myb{w}}{\myb{f}}}}
1140 \UnaryInfC{$\myjud{\myrec{\myse{u}}{\myb{w}}{\myse{P}}{\myse{p}}}{\mysub{\myse{P}}{\myb{w}}{\myse{u}}}$}
1144 Finally, the well-order type, or in short $\mytyc{W}$-type, which will
1145 let us represent inductive data in a general (but clumsy) way. The core
1149 \section{The struggle for equality}
1150 \label{sec:equality}
1152 In the previous section we saw how a type checker (or a human) needs a
1153 notion of \emph{definitional equality}. Beyond this meta-theoretic
1154 notion, in this section we will explore the ways of expressing equality
1155 \emph{inside} the theory, as a reasoning tool available to the user.
1156 This area is the main concern of this thesis, and in general a very
1157 active research topic, since we do not have a fully satisfactory
1158 solution, yet. As in the previous section, everything presented is
1159 formalised in Agda in appendix \ref{app:agda-itt}.
1161 \subsection{Propositional equality}
1164 \begin{minipage}{0.5\textwidth}
1167 \begin{array}{r@{\ }c@{\ }l}
1168 \mytmsyn & ::= & \cdots \\
1169 & | & \mytmsyn \mypeq{\mytmsyn} \mytmsyn \mysynsep
1170 \myapp{\myrefl}{\mytmsyn} \\
1171 & | & \myjeq{\mytmsyn}{\mytmsyn}{\mytmsyn}
1176 \begin{minipage}{0.5\textwidth}
1177 \mydesc{reduction:}{\mytmsyn \myred \mytmsyn}{
1179 \myjeq{\myse{P}}{(\myapp{\myrefl}{\mytmm})}{\mytmn} \myred \mytmn
1185 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1186 \AxiomC{$\myjud{\mytya}{\mytyp_l}$}
1187 \AxiomC{$\myjud{\mytmm}{\mytya}$}
1188 \AxiomC{$\myjud{\mytmn}{\mytya}$}
1189 \TrinaryInfC{$\myjud{\mytmm \mypeq{\mytya} \mytmn}{\mytyp_l}$}
1195 \AxiomC{$\begin{array}{c}\ \\\myjud{\mytmm}{\mytya}\hspace{1.1cm}\mytmm \mydefeq \mytmn\end{array}$}
1196 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmm}}{\mytmm \mypeq{\mytya} \mytmn}$}
1201 \myjud{\myse{P}}{\myfora{\myb{x}\ \myb{y}}{\mytya}{\myfora{q}{\myb{x} \mypeq{\mytya} \myb{y}}{\mytyp_l}}} \\
1202 \myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}\hspace{1.1cm}\myjud{\myse{p}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}}
1205 \UnaryInfC{$\myjud{\myjeq{\myse{P}}{\myse{q}}{\myse{p}}}{\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmn}}{q}}$}
1210 To express equality between two terms inside ITT, the obvious way to do so is
1211 to have the equality construction to be a type-former. Here we present what
1212 has survived as the dominating form of equality in systems based on ITT up to
1215 Our type former is $\mypeq{\mytya}$, which given a type (in this case
1216 $\mytya$) relates equal terms of that type. $\mypeq{}$ has one introduction
1217 rule, $\myrefl$, which introduces an equality relation between definitionally
1220 Finally, we have one eliminator for $\mypeq{}$, $\myjeqq$. $\myjeq{\myse{P}}{\myse{q}}{\myse{p}}$ takes
1222 \item $\myse{P}$, a predicate working with two terms of a certain type (say
1223 $\mytya$) and a proof of their equality
1224 \item $\myse{q}$, a proof that two terms in $\mytya$ (say $\myse{m}$ and
1225 $\myse{n}$) are equal
1226 \item and $\myse{p}$, an inhabitant of $\myse{P}$ applied to $\myse{m}$, plus
1227 the trivial proof by reflexivity showing that $\myse{m}$ is equal to itself
1229 Given these ingredients, $\myjeqq$ retuns a member of $\myse{P}$ applied to
1230 $\mytmm$, $\mytmn$, and $\myse{q}$. In other words $\myjeqq$ takes a
1231 witness that $\myse{P}$ works with \emph{definitionally equal} terms, and
1232 returns a witness of $\myse{P}$ working with \emph{propositionally equal}
1233 terms. Invokations of $\myjeqq$ will vanish when the equality proofs will
1234 reduce to invocations to reflexivity, at which point the arguments must be
1235 definitionally equal, and thus the provided
1236 $\myapp{\myapp{\myapp{\myse{P}}{\mytmm}}{\mytmm}}{(\myapp{\myrefl}{\mytmm})}$
1239 While the $\myjeqq$ rule is slightly convoluted, ve can derive many more
1240 `friendly' rules from it, for example a more obvious `substitution' rule, that
1241 replaces equal for equal in predicates:
1244 \myfun{subst} : \myfora{\myb{A}}{\mytyp}{\myfora{\myb{P}}{\myb{A} \myarr \mytyp}{\myfora{\myb{x}\ \myb{y}}{\myb{A}}{\myb{x} \mypeq{\myb{A}} \myb{y} \myarr \myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{\myb{y}}}}} \\
1245 \myfun{subst}\myappsp \myb{A}\myappsp\myb{P}\myappsp\myb{x}\myappsp\myb{y}\myappsp\myb{q}\myappsp\myb{p} \mapsto
1246 \myjeq{(\myabs{\myb{x}\ \myb{y}\ \myb{q}}{\myapp{\myb{P}}{\myb{y}}})}{\myb{p}}{\myb{q}}
1249 Once we have $\myfun{subst}$, we can easily prove more familiar laws regarding
1250 equality, such as symmetry, transitivity, and a congruence law.
1254 \subsection{Common extensions}
1256 Our definitional equality can be made larger in various ways, here we
1257 review some common extensions.
1259 \subsubsection{Congruence laws and $\eta$-expansion}
1261 A simple type-directed check that we can do on functions and records is
1262 $\eta$-expansion. We can then have
1264 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1266 \AxiomC{$\myjud{f \mydefeq (\myabss{\myb{x}}{\mytya}{\myapp{\myse{g}}{\myb{x}}})}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1267 \UnaryInfC{$\myjud{\myse{f} \mydefeq \myse{g}}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1270 \AxiomC{$\myjud{\mytmm \mydefeq \mypair{\myapp{\myfst}{\mytmn}}{\myapp{\mysnd}{\mytmn}}}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1271 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myexi{\myb{x}}{\mytya}{\mytyb}}$}
1277 \AxiomC{$\myjud{\mytmm}{\myunit}$}
1278 \AxiomC{$\myjud{\mytmn}{\myunit}$}
1279 \BinaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\myunit}$}
1283 % \mydesc{definitional equality:}{\mytmsyn \mydefeq \mytmsyn}{
1284 % \begin{tabular}{cc}
1292 \subsubsection{Uniqueness of identity proofs}
1294 % TODO reference the fact that J does not imply J
1295 % TODO mention univalence
1298 \mydesc{definitional equality:}{\myjud{\mytmm \mydefeq \mytmn}{\mytmsyn}}{
1301 \myjud{\myse{P}}{\myfora{\myb{x}}{\mytya}{\myb{x} \mypeq{\mytya} \myb{x} \myarr \mytyp}} \\\
1302 \myjud{\myse{p}}{\myfora{\myb{x}}{\mytya}{\myse{P} \myappsp \myb{x} \myappsp \myb{x} \myappsp (\myrefl \myapp \myb{x})}} \hspace{1cm}
1303 \myjud{\mytmt}{\mytya} \hspace{1cm}
1304 \myjud{\myse{q}}{\mytmt \mypeq{\mytya} \mytmt}
1307 \UnaryInfC{$\myjud{\myfun{K} \myappsp \myse{P} \myappsp \myse{p} \myappsp \myse{t} \myappsp \myse{q}}{\myse{P} \myappsp \mytmt \myappsp \myse{q}}$}
1311 \subsection{Limitations}
1313 \epigraph{\emph{Half of my time spent doing research involves thinking up clever
1314 schemes to avoid needing functional extensionality.}}{@larrytheliquid}
1316 However, propositional equality as described is quite restricted when
1317 reasoning about equality beyond the term structure, which is what definitional
1318 equality gives us (extension notwithstanding).
1320 The problem is best exemplified by \emph{function extensionality}. In
1321 mathematics, we would expect to be able to treat functions that give equal
1322 output for equal input as the same. When reasoning in a mechanised framework
1323 we ought to be able to do the same: in the end, without considering the
1324 operational behaviour, all functions equal extensionally are going to be
1325 replaceable with one another.
1327 However this is not the case, or in other words with the tools we have we have
1330 \myfun{ext} : \myfora{\myb{A}\ \myb{B}}{\mytyp}{\myfora{\myb{f}\ \myb{g}}{
1331 \myb{A} \myarr \myb{B}}{
1332 (\myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{\myb{B}} \myapp{\myb{g}}{\myb{x}}}) \myarr
1333 \myb{f} \mypeq{\myb{A} \myarr \myb{B}} \myb{g}
1337 To see why this is the case, consider the functions
1338 {\small\[\myabs{\myb{x}}{0 \mathrel{\myfun{+}} \myb{x}}$ and $\myabs{\myb{x}}{\myb{x} \mathrel{\myfun{+}} 0}\]}
1339 where $\myfun{+}$ is defined by recursion on the first argument,
1340 gradually destructing it to build up successors of the second argument.
1341 The two functions are clearly extensionally equal, and we can in fact
1344 \myfora{\myb{x}}{\mynat}{(0 \mathrel{\myfun{+}} \myb{x}) \mypeq{\mynat} (\myb{x} \mathrel{\myfun{+}} 0)}
1346 By analysis on the $\myb{x}$. However, the two functions are not
1347 definitionally equal, and thus we won't be able to get rid of the
1350 For the reasons above, theories that offer a propositional equality
1351 similar to what we presented are called \emph{intensional}, as opposed
1352 to \emph{extensional}. Most systems in wide use today (such as Agda,
1353 Coq, and Epigram) are of this kind.
1355 This is quite an annoyance that often makes reasoning awkward to execute. It
1356 also extends to other fields, for example proving bisimulation between
1357 processes specified by coinduction, or in general proving equivalences based
1358 on the behaviour on a term.
1360 \subsection{Equality reflection}
1362 One way to `solve' this problem is by identifying propositional equality with
1363 definitional equality:
1365 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1366 \AxiomC{$\myjud{\myse{q}}{\mytmm \mypeq{\mytya} \mytmn}$}
1367 \UnaryInfC{$\myjud{\mytmm \mydefeq \mytmn}{\mytya}$}
1371 This rule takes the name of \emph{equality reflection}, and is a very
1372 different rule from the ones we saw up to now: it links a typing judgement
1373 internal to the type theory to a meta-theoretic judgement that the type
1374 checker uses to work with terms. It is easy to see the dangerous consequences
1377 \item The rule is syntax directed, and the type checker is presumably expected
1378 to come up with equality proofs when needed.
1379 \item More worryingly, type checking becomes undecidable also because
1380 computing under false assumptions becomes unsafe.
1381 Consider for example
1383 \myabss{\myb{q}}{\mytya \mypeq{\mytyp} (\mytya \myarr \mytya)}{\myhole{?}}
1385 Using the assumed proof in tandem with equality reflection we could easily
1386 write a classic Y combinator, sending the compiler into a loop.
1389 Given these facts theories employing equality reflection, like NuPRL
1390 \citep{NuPRL}, carry the derivations that gave rise to each typing judgement
1391 to keep the systems manageable. % TODO more info, problems with that.
1393 For all its faults, equality reflection does allow us to prove extensionality,
1394 using the extensions we gave above. Assuming that $\myctx$ contains
1395 {\small\[\myb{A}, \myb{B} : \mytyp; \myb{f}, \myb{g} : \myb{A} \myarr \myb{B}; \myb{q} : \myfora{\myb{x}}{\myb{A}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\]}
1399 \AxiomC{$\hspace{1.1cm}\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{q}}{\myb{x}}}{\myapp{\myb{f}}{\myb{x}} \mypeq{} \myapp{\myb{g}}{\myb{x}}}\hspace{1.1cm}$}
1400 \RightLabel{equality reflection}
1401 \UnaryInfC{$\myjudd{\myctx; \myb{x} : \myb{A}}{\myapp{\myb{f}}{\myb{x}} \mydefeq \myapp{\myb{g}}{\myb{x}}}{\myb{B}}$}
1402 \RightLabel{congruence for $\lambda$s}
1403 \UnaryInfC{$\myjud{(\myabs{\myb{x}}{\myapp{\myb{f}}{\myb{x}}}) \mydefeq (\myabs{\myb{x}}{\myapp{\myb{g}}{\myb{x}}})}{\myb{A} \myarr \myb{B}}$}
1404 \RightLabel{$\eta$-law for $\lambda$}
1405 \UnaryInfC{$\hspace{1.45cm}\myjud{\myb{f} \mydefeq \myb{g}}{\myb{A} \myarr \myb{B}}\hspace{1.45cm}$}
1406 \RightLabel{$\myrefl$}
1407 \UnaryInfC{$\myjud{\myapp{\myrefl}{\myb{f}}}{\myb{f} \mypeq{} \myb{g}}$}
1410 Now, the question is: do we need to give up well-behavedness of our theory to
1411 gain extensionality?
1413 \subsection{Some alternatives}
1415 % TODO add `extentional axioms' (Hoffman), setoid models (Thorsten)
1417 \section{Observational equality}
1420 A recent development by \citet{Altenkirch2007}, \emph{Observational Type
1421 Theory} (OTT), promises to keep the well behavedness of ITT while
1422 being able to gain many useful equality proofs\footnote{It is suspected
1423 that OTT gains \emph{all} the equality proofs of ETT, but no proof
1424 exists yet.}, including function extensionality. The main idea is to
1425 give the user the possibility to \emph{coerce} (or transport) values
1426 from a type $\mytya$ to a type $\mytyb$, if the type checker can prove
1427 structurally that $\mytya$ and $\mytya$ are equal; and providing a
1428 value-level equality based on similar principles. Here we give an
1429 exposition which follows closely the original paper.
1431 \subsection{A simpler theory, a propositional fragment}
1434 $\mytyp_l$ is replaced by $\mytyp$. \\\ \\
1436 \begin{array}{r@{\ }c@{\ }l}
1437 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \mysynsep
1438 \myITE{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1439 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn
1440 \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
1445 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1447 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1448 \UnaryInfC{$\myjud{\myprdec{\myse{P}}}{\mytyp}$}
1451 \AxiomC{$\myjud{\mytmt}{\mybool}$}
1452 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1453 \AxiomC{$\myjud{\mytyb}{\mytyp}$}
1454 \TrinaryInfC{$\myjud{\myITE{\mytmt}{\mytya}{\mytyb}}{\mytyp}$}
1459 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1461 \AxiomC{\phantom{$\myjud{\myse{P}}{\myprop}$}}
1462 \UnaryInfC{$\myjud{\mytop}{\myprop}$}
1464 \UnaryInfC{$\myjud{\mybot}{\myprop}$}
1467 \AxiomC{$\myjud{\myse{P}}{\myprop}$}
1468 \AxiomC{$\myjud{\myse{Q}}{\myprop}$}
1469 \BinaryInfC{$\myjud{\myse{P} \myand \myse{Q}}{\myprop}$}
1471 \UnaryInfC{\phantom{$\myjud{\mybot}{\myprop}$}}
1477 \AxiomC{$\myjud{\myse{A}}{\mytyp}$}
1478 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\myse{P}}{\myprop}$}
1479 \BinaryInfC{$\myjud{\myprfora{\myb{x}}{\mytya}{\myse{P}}}{\myprop}$}
1483 Our foundation will be a type theory like the one of section
1484 \ref{sec:itt}, with only one level: $\mytyp_0$. In this context we will
1485 drop the $0$ and call $\mytyp_0$ $\mytyp$. Moreover, since the old
1486 $\myfun{if}\myarg\myfun{then}\myarg\myfun{else}$ was able to return
1487 types thanks to the hierarchy (which is gone), we need to reintroduce an
1488 ad-hoc conditional for types, where the reduction rule is the obvious
1491 However, we have an addition: a universe of \emph{propositions},
1492 $\myprop$. $\myprop$ isolates a fragment of types at large, and
1493 indeed we can `inject' any $\myprop$ back in $\mytyp$ with $\myprdec{\myarg}$: \\
1494 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
1497 \begin{array}{l@{\ }c@{\ }l}
1498 \myprdec{\mybot} & \myred & \myempty \\
1499 \myprdec{\mytop} & \myred & \myunit
1504 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
1505 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
1506 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
1507 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
1512 Propositions are what we call the types of \emph{proofs}, or types
1513 whose inhabitants contain no `data', much like $\myunit$. The goal of
1514 doing this is twofold: erasing all top-level propositions when
1515 compiling; and to identify all equivalent propositions as the same, as
1518 Why did we choose what we have in $\myprop$? Given the above
1519 criteria, $\mytop$ obviously fits the bill. A pair of propositions
1520 $\myse{P} \myand \myse{Q}$ still won't get us data. Finally, if
1521 $\myse{P}$ is a proposition and we have
1522 $\myprfora{\myb{x}}{\mytya}{\myse{P}}$ , the decoding will be a
1523 function which returns propositional content. The only threat is
1524 $\mybot$, by which we can fabricate anything we want: however if we
1525 are consistent there will be nothing of type $\mybot$ at the top
1526 level, which is what we care about regarding proof erasure.
1528 \subsection{Equality proofs}
1532 \begin{array}{r@{\ }c@{\ }l}
1533 \mytmsyn & ::= & \cdots \mysynsep
1534 \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
1535 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
1536 \myprsyn & ::= & \cdots \mysynsep \mytmsyn \myeq \mytmsyn \mysynsep
1537 \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn}
1542 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1544 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1545 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1546 \BinaryInfC{$\myjud{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}$}
1549 \AxiomC{$\myjud{\myse{P}}{\myprdec{\mytya \myeq \mytyb}}$}
1550 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1551 \BinaryInfC{$\myjud{\mycohh{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\myprdec{\myjm{\mytmt}{\mytya}{\mycoee{\mytya}{\mytyb}{\myse{P}}{\mytmt}}{\mytyb}}}$}
1557 \mydesc{propositions:}{\myjud{\myprsyn}{\myprop}}{
1562 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\myse{B}}{\mytyp}
1565 \UnaryInfC{$\myjud{\mytya \myeq \mytyb}{\myprop}$}
1570 \myjud{\myse{A}}{\mytyp} \hspace{1cm} \myjud{\mytmm}{\myse{A}} \\
1571 \myjud{\myse{B}}{\mytyp} \hspace{1cm} \myjud{\mytmn}{\myse{B}}
1574 \UnaryInfC{$\myjud{\myjm{\mytmm}{\myse{A}}{\mytmn}{\myse{B}}}{\myprop}$}
1581 While isolating a propositional universe as presented can be a useful
1582 exercises on its own, what we are really after is a useful notion of
1583 equality. In OTT we want to maintain the notion that things judged to
1584 be equal are still always repleaceable for one another with no
1585 additional changes. Note that this is not the same as saying that they
1586 are definitionally equal, since as we saw extensionally equal functions,
1587 while satisfying the above requirement, are not definitionally equal.
1589 Towards this goal we introduce two equality constructs in
1590 $\myprop$---the fact that they are in $\myprop$ indicates that they
1591 indeed have no computational content. The first construct, $\myarg
1592 \myeq \myarg$, relates types, the second,
1593 $\myjm{\myarg}{\myarg}{\myarg}{\myarg}$, relates values. The
1594 value-level equality is different from our old propositional equality:
1595 instead of ranging over only one type, we might form equalities between
1596 values of different types---the usefulness of this construct will be
1597 clear soon. In the literature this equality is known as `heterogeneous'
1598 or `John Major', since
1601 John Major's `classless society' widened people's aspirations to
1602 equality, but also the gap between rich and poor. After all, aspiring
1603 to be equal to others than oneself is the politics of envy. In much
1604 the same way, forms equations between members of any type, but they
1605 cannot be treated as equals (ie substituted) unless they are of the
1606 same type. Just as before, each thing is only equal to
1607 itself. \citep{McBride1999}.
1610 Correspondingly, at the term level, $\myfun{coe}$ (`coerce') lets us
1611 transport values between equal types; and $\myfun{coh}$ (`coherence')
1612 guarantees that $\myfun{coe}$ respects the value-level equality, or in
1613 other words that it really has no computational component: if we
1614 transport $\mytmm : \mytya$ to $\mytmn : \mytyb$, $\mytmm$ and $\mytmn$
1615 will still be the same.
1617 Before introducing the core ideas that make OTT work, let us distinguish
1618 between \emph{canonical} and \emph{neutral} types. Canonical types are
1619 those arising from the ground types ($\myempty$, $\myunit$, $\mybool$)
1620 and the three type formers ($\myarr$, $\myprod$, $\mytyc{W}$). Neutral
1621 types are those formed by
1622 $\myfun{If}\myarg\myfun{Then}\myarg\myfun{Else}\myarg$.
1623 Correspondingly, canonical terms are those inhabiting canonical types
1624 ($\mytt$, $\mytrue$, $\myfalse$, $\myabss{\myb{x}}{\mytya}{\mytmt}$,
1625 ...), and neutral terms those formed by eliminators\footnote{Using the
1626 terminology from section \ref{sec:types}, we'd say that canonical
1627 terms are in \emph{weak head normal form}.}. In the current system
1628 (and hopefully in well-behaved systems), all closed terms reduce to a
1629 canonical term, and all canonical types are inhabited by canonical
1632 \subsubsection{Type equality, and coercions}
1634 The plan is to decompose type-level equalities between canonical types
1635 into decodable propositions containing equalities regarding the
1636 subterms, and to use coerce recursively on the subterms using the
1637 generated equalities. This interplay between type equalities and
1638 \myfun{coe} ensures that invocations of $\myfun{coe}$ will vanish when
1639 we have evidence of the structural equality of the types we are
1640 transporting terms across. If the type is neutral, the equality won't
1641 reduce and thus $\myfun{coe}$ won't reduce either. If we come an
1642 equality between different canonical types, then we reduce the equality
1643 to bottom, making sure that no such proof can exist, and providing an
1644 `escape hatch' in $\myfun{coe}$.
1648 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1650 \begin{array}{c@{\ }c@{\ }c@{\ }l}
1651 \myempty & \myeq & \myempty & \myred \mytop \\
1652 \myunit & \myeq & \myunit & \myred \mytop \\
1653 \mybool & \myeq & \mybool & \myred \mytop \\
1654 \myexi{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myexi{\myb{x_2}}{\mytya_2}{\mytya_2} & \myred \\
1656 \myind{2} \mytya_1 \myeq \mytyb_1 \myand
1657 \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}} \myimpl \mytyb_1 \myeq \mytyb_2}
1659 \myfora{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myfora{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1660 \myw{\myb{x_1}}{\mytya_1}{\mytyb_1} & \myeq & \myw{\myb{x_2}}{\mytya_2}{\mytyb_2} & \myred \cdots \\
1661 \mytya & \myeq & \mytyb & \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1666 \mydesc{reduction}{\mytmsyn \myred \mytmsyn}{
1668 \begin{array}[t]{@{}l@{\ }l@{\ }l@{\ }l@{\ }l@{\ }c@{\ }l@{\ }}
1669 \mycoe & \myempty & \myempty & \myse{Q} & \myse{t} & \myred & \myse{t} \\
1670 \mycoe & \myunit & \myunit & \myse{Q} & \mytt & \myred & \mytt \\
1671 \mycoe & \mybool & \mybool & \myse{Q} & \mytrue & \myred & \mytrue \\
1672 \mycoe & \mybool & \mybool & \myse{Q} & \myfalse & \myred & \myfalse \\
1673 \mycoe & (\myexi{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1674 (\myexi{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1675 \mytmt_1 & \myred & \\
1677 \myind{2}\begin{array}[t]{l@{\ }l@{\ }c@{\ }l}
1678 \mysyn{let} & \myb{\mytmm_1} & \mapsto & \myapp{\myfst}{\mytmt_1} : \mytya_1 \\
1679 & \myb{\mytmn_1} & \mapsto & \myapp{\mysnd}{\mytmt_1} : \mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \\
1680 & \myb{Q_A} & \mapsto & \myapp{\myfst}{\myse{Q}} : \mytya_1 \myeq \mytya_2 \\
1681 & \myb{\mytmm_2} & \mapsto & \mycoee{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}} : \mytya_2 \\
1682 & \myb{Q_B} & \mapsto & (\myapp{\mysnd}{\myse{Q}}) \myappsp \myb{\mytmm_1} \myappsp \myb{\mytmm_2} \myappsp (\mycohh{\mytya_1}{\mytya_2}{\myb{Q_A}}{\myb{\mytmm_1}}) : \\ & & & \myprdec{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}} \myeq \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}} \\
1683 & \myb{\mytmn_2} & \mapsto & \mycoee{\mysub{\mytyb_1}{\myb{x_1}}{\myb{\mytmm_1}}}{\mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}}}{\myb{Q_B}}{\myb{\mytmn_1}} : \mysub{\mytyb_2}{\myb{x_2}}{\myb{\mytmm_2}} \\
1684 \mysyn{in} & \multicolumn{3}{@{}l}{\mypair{\myb{\mytmm_2}}{\myb{\mytmn_2}}}
1687 \mycoe & (\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1688 (\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1692 \mycoe & (\myw{\myb{x_1}}{\mytya_1}{\mytyb_1}) &
1693 (\myw{\myb{x_2}}{\mytya_2}{\mytyb_2}) & \myse{Q} &
1697 \mycoe & \mytya & \mytyb & \myse{Q} & \mytmt & \myred & \\
1699 \myind{2}\myapp{\myabsurd{\mytyb}}{\myse{Q}}\ \text{if $\mytya$ and $\mytyb$ are canonical.}
1704 \caption{Reducing type equalities, and using them when
1705 $\myfun{coe}$rcing.}
1709 Figure \ref{fig:eqred} illustrates this idea in practice. For ground
1710 types, the proof is the trivial element, and \myfun{coe} is the
1711 identity. For the three type binders, things are similar but subtly
1712 different---the choices we make in the type equality are dictated by
1713 the desire of writing the $\myfun{coe}$ in a natural way.
1715 $\myprod$ is the easiest case: we decompose the proof into proofs that
1716 the first element's types are equal ($\mytya_1 \myeq \mytya_2$), and a
1717 proof that given equal values in the first element, the types of the
1718 second elements are equal too
1719 ($\myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{\myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2}}
1720 \myimpl \mytyb_1 \myeq \mytyb_2}$)\footnote{We are using $\myimpl$ to
1721 indicate a $\forall$ where we discard the first value. Also note that
1722 the $\myb{x_1}$ in the $\mytyb_1$ inside the $\forall$ is re-bound to
1723 the quantification, and similarly for $\myb{x_2}$ and $\mytyb_2$.}.
1724 This also explains the need for heterogeneous equality, since in the
1725 second proof it would be awkward to express the fact that $\myb{A_1}$ is
1726 the same as $\myb{A_2}$. In the respective $\myfun{coe}$ case, since
1727 the types are canonical, we know at this point that the proof of
1728 equality is a pair of the shape described above. Thus, we can
1729 immediately coerce the first element of the pair using the first element
1730 of the proof, and then instantiate the second element with the two first
1731 elements and a proof by coherence of their equality, since we know that
1732 the types are equal. The cases for the other binders are omitted for
1733 brevity, but they follow the same principle.
1735 \subsubsection{$\myfun{coe}$, laziness, and $\myfun{coh}$erence}
1737 It is important to notice that in the reduction rules for $\myfun{coe}$
1738 are never obstructed by the proofs: with the exception of comparisons
1739 between different canonical types we never pattern match on the pairs,
1740 but always look at the projections. This means that, as long as we are
1741 consistent, and thus as long as we don't have $\mybot$-inducing proofs,
1742 we can add propositional axioms for equality and $\myfun{coe}$ will
1743 still compute. Thus, we can take $\myfun{coh}$ as axiomatic, and we can
1744 add back familiar useful equality rules:
1746 \mydesc{typing:}{\myjud{\mytmsyn}{\mytmsyn}}{
1747 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1748 \UnaryInfC{$\myjud{\myapp{\myrefl}{\mytmt}}{\myprdec{\myjm{\myb{x}}{\myb{\mytya}}{\myb{x}}{\myb{\mytya}}}}$}
1753 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1754 \AxiomC{$\myjudd{\myctx; \myb{x} : \mytya}{\mytyb}{\mytyp}$}
1755 \BinaryInfC{$\myjud{\mytyc{R} \myappsp (\myb{x} {:} \mytya) \myappsp \mytyb}{\myfora{\myb{y}\, \myb{z}}{\mytya}{\myprdec{\myjm{\myb{y}}{\mytya}{\myb{z}}{\mytya} \myimpl \mysub{\mytyb}{\myb{x}}{\myb{y}} \myeq \mysub{\mytyb}{\myb{x}}{\myb{z}}}}}$}
1759 $\myrefl$ is the equivalent of the reflexivity rule in propositional
1760 equality, and $\mytyc{R}$ asserts that if we have a we have a $\mytyp$
1761 abstracting over a value we can substitute equal for equal---this lets
1762 us recover $\myfun{subst}$. Note that while we need to provide ad-hoc
1763 rules in the restricted, non-hierarchical theory that we have, if our
1764 theory supports abstraction over $\mytyp$s we can easily add these
1765 axioms as abstracted variables.
1767 \subsubsection{Value-level equality}
1769 \mydesc{equality reduction:}{\myprsyn \myred \myprsyn}{
1771 \begin{array}{r@{ }c@{\ }c@{\ }c@{}l@{\ }c@{\ }r@{}c@{\ }c@{\ }c@{}l@{\ }l}
1772 (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty &) & \myred \mytop \\
1773 (&\mytmt_1 & : & \myempty&) & \myeq & (&\mytmt_2 & : & \myempty&) & \myred \mytop \\
1774 (&\mytrue & : & \mybool&) & \myeq & (&\mytrue & : & \mybool&) & \myred \mytop \\
1775 (&\myfalse & : & \mybool&) & \myeq & (&\myfalse & : & \mybool&) & \myred \mytop \\
1776 (&\mytmt_1 & : & \mybool&) & \myeq & (&\mytmt_1 & : & \mybool&) & \myred \mybot \\
1777 (&\mytmt_1 & : & \myexi{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_2 & : & \myexi{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1778 & \multicolumn{11}{@{}l}{
1779 \myind{2} \myjm{\myapp{\myfst}{\mytmt_1}}{\mytya_1}{\myapp{\myfst}{\mytmt_2}}{\mytya_2} \myand
1780 \myjm{\myapp{\mysnd}{\mytmt_1}}{\mysub{\mytyb_1}{\myb{x_1}}{\myapp{\myfst}{\mytmt_1}}}{\myapp{\mysnd}{\mytmt_2}}{\mysub{\mytyb_2}{\myb{x_2}}{\myapp{\myfst}{\mytmt_2}}}
1782 (&\myse{f}_1 & : & \myfora{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\myse{f}_2 & : & \myfora{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \\
1783 & \multicolumn{11}{@{}l}{
1784 \myind{2} \myprfora{\myb{x_1}}{\mytya_1}{\myprfora{\myb{x_2}}{\mytya_2}{
1785 \myjm{\myb{x_1}}{\mytya_1}{\myb{x_2}}{\mytya_2} \myimpl
1786 \myjm{\myapp{\myse{f}_1}{\myb{x_1}}}{\mytyb_1}{\myapp{\myse{f}_2}{\myb{x_2}}}{\mytyb_2}
1789 (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_1}{\myb{x_1}}{\mytyb_1}&) & \myeq & (&\mytmt_1 \mynodee \myse{f}_1 & : & \myw{\mytya_2}{\myb{x_2}}{\mytyb_2}&) & \myred \cdots \\
1790 (&\mytmt_1 & : & \mytya_1&) & \myeq & (&\mytmt_2 & : & \mytya_2 &) & \myred \\
1791 & \multicolumn{11}{@{}l}{
1792 \myind{2} \mybot\ \text{if $\mytya_1$ and $\mytya_2$ are canonical.}
1798 As with type-level equality, we want value-level equality to reduce
1799 based on the structure of the compared terms.
1801 \subsection{Proof irrelevance}
1803 % \section{Augmenting ITT}
1804 % \label{sec:practical}
1806 % \subsection{A more liberal hierarchy}
1808 % \subsection{Type inference}
1810 % \subsubsection{Bidirectional type checking}
1812 % \subsubsection{Pattern unification}
1814 % \subsection{Pattern matching and explicit fixpoints}
1816 % \subsection{Induction-recursion}
1818 % \subsection{Coinduction}
1820 % \subsection{Dealing with partiality}
1822 % \subsection{Type holes}
1824 \section{\mykant : the theory}
1825 \label{sec:kant-theory}
1827 \mykant\ is an interactive theorem prover developed as part of this thesis.
1828 The plan is to present a core language which would be capable of serving as
1829 the basis for a more featureful system, while still presenting interesting
1830 features and more importantly observational equality.
1832 The author learnt the hard way the implementations challenges for such a
1833 project, and while there is a solid and working base to work on, observational
1834 equality is not currently implemented. However, a detailed plan on how to add
1835 it this functionality is provided, and should not prove to be too much work.
1837 The features currently implemented in \mykant\ are:
1840 \item[Full dependent types] As we would expect, we have dependent a system
1841 which is as expressive as the `best' corner in the lambda cube described in
1842 section \ref{sec:itt}.
1844 \item[Implicit, cumulative universe hierarchy] The user does not need to
1845 specify universe level explicitly, and universes are \emph{cumulative}.
1847 \item[User defined data types and records] Instead of forcing the user to
1848 choose from a restricted toolbox, we let her define inductive data types,
1849 with associated primitive recursion operators; or records, with associated
1850 projections for each field.
1852 \item[Bidirectional type checking] While no `fancy' inference via unification
1853 is present, we take advantage of an type synthesis system in the style of
1854 \cite{Pierce2000}, extending the concept for user defined data types.
1856 \item[Type holes] When building up programs interactively, it is useful to
1857 leave parts unfinished while exploring the current context. This is what
1861 The planned features are:
1864 \item[Observational equality] As described in section \ref{sec:ott} but
1865 extended to work with the type hierarchy and to admit equality between
1866 arbitrary data types.
1868 \item[Coinductive data] ...
1871 We will analyse the features one by one, along with motivations and tradeoffs
1872 for the design decisions made.
1874 \subsection{Bidirectional type checking}
1876 We start by describing bidirectional type checking since it calls for fairly
1877 different typing rules that what we have seen up to now. The idea is to have
1878 two kind of terms: terms for which a type can always be inferred, and terms
1879 that need to be checked against a type. A nice observation is that this
1880 duality runs through the semantics of the terms: data destructors (function
1881 application, record projections, primitive re cursors) \emph{infer} types,
1882 while data constructors (abstractions, record/data types data constructors)
1883 need to be checked. In the literature these terms are respectively known as
1885 To introduce the concept and notation, we will revisit the STLC in a
1886 bidirectional style. The presentation follows \cite{Loh2010}.
1888 % TODO do this --- is it even necessary
1892 \subsection{Base terms and types}
1894 Let us begin by describing the primitives available without the user
1895 defining any data types, and without equality. The way we handle
1896 variables and substitution is left unspecified, and explained in section
1897 \ref{sec:term-repr}, along with other implementation issues. We are
1898 also going to give an account of the implicit type hierarchy separately
1899 in section \ref{sec:term-hierarchy}, so as not to clutter derivation
1900 rules too much, and just treat types as impredicative for the time
1905 \begin{array}{r@{\ }c@{\ }l}
1906 \mytmsyn & ::= & \mynamesyn \mysynsep \mytyp \\
1907 & | & \myfora{\myb{x}}{\mytmsyn}{\mytmsyn} \mysynsep
1908 \myabs{\myb{x}}{\mytmsyn} \mysynsep
1909 (\myapp{\mytmsyn}{\mytmsyn}) \mysynsep
1910 (\myann{\mytmsyn}{\mytmsyn}) \\
1911 \mynamesyn & ::= & \myb{x} \mysynsep \myfun{f}
1916 The syntax for our calculus includes just two basic constructs:
1917 abstractions and $\mytyp$s. Everything else will be provided by
1918 user-definable constructs. Since we let the user define values, we will
1919 need a context capable of carrying the body of variables along with
1920 their type. Bound names and defined names are treated separately in the
1921 syntax, and while both can be associated to a type in the context, only
1922 defined names can be associated with a body:
1924 \mydesc{context validity:}{\myvalid{\myctx}}{
1925 \begin{tabular}{ccc}
1926 \AxiomC{\phantom{$\myjud{\mytya}{\mytyp_l}$}}
1927 \UnaryInfC{$\myvalid{\myemptyctx}$}
1930 \AxiomC{$\myjud{\mytya}{\mytyp}$}
1931 \AxiomC{$\mynamesyn \not\in \myctx$}
1932 \BinaryInfC{$\myvalid{\myctx ; \mynamesyn : \mytya}$}
1935 \AxiomC{$\myjud{\mytmt}{\mytya}$}
1936 \AxiomC{$\myfun{f} \not\in \myctx$}
1937 \BinaryInfC{$\myvalid{\myctx ; \myfun{f} \mapsto \mytmt : \mytya}$}
1942 Now we can present the reduction rules, which are unsurprising. We have
1943 the usual function application ($\beta$-reduction), but also a rule to
1944 replace names with their bodies ($\delta$-reduction), and one to discard
1945 type annotations. For this reason reduction is done in-context, as
1946 opposed to what we have seen in the past:
1948 \mydesc{reduction:}{\myctx \vdash \mytmsyn \myred \mytmsyn}{
1949 \begin{tabular}{ccc}
1950 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1951 \UnaryInfC{$\myctx \vdash \myapp{(\myabs{\myb{x}}{\mytmm})}{\mytmn}
1952 \myred \mysub{\mytmm}{\myb{x}}{\mytmn}$}
1955 \AxiomC{$\myfun{f} \mapsto \mytmt : \mytya \in \myctx$}
1956 \UnaryInfC{$\myctx \vdash \myfun{f} \myred \mytmt$}
1959 \AxiomC{\phantom{$\myb{x} \mapsto \mytmt : \mytya \in \myctx$}}
1960 \UnaryInfC{$\myctx \vdash \myann{\mytmm}{\mytya} \myred \mytmm$}
1965 We can now give types to our terms. The type of names, both defined and
1966 abstract, is inferred. The type of applications is inferred too,
1967 propagating types down the applied term. Abstractions are checked.
1968 Finally, we have a rule to check the type of an inferrable term. We
1969 defer the question of term equality (which is needed for type checking)
1970 to section \label{sec:kant-irr}.
1972 \mydesc{typing:}{\myctx \vdash \mytmsyn \Leftrightarrow \mytmsyn}{
1973 \begin{tabular}{ccc}
1974 \AxiomC{$\myse{name} : A \in \myctx$}
1975 \UnaryInfC{$\myinf{\myse{name}}{A}$}
1978 \AxiomC{$\myfun{f} \mapsto \mytmt : A \in \myctx$}
1979 \UnaryInfC{$\myinf{\myfun{f}}{A}$}
1982 \AxiomC{$\myinf{\mytmt}{\mytya}$}
1983 \UnaryInfC{$\mychk{\myann{\mytmt}{\mytya}}{\mytya}$}
1988 \begin{tabular}{ccc}
1989 \AxiomC{$\myinf{\mytmm}{\myfora{\myb{x}}{\mytya}{\mytyb}}$}
1990 \AxiomC{$\mychk{\mytmn}{\mytya}$}
1991 \BinaryInfC{$\myinf{\myapp{\mytmm}{\mytmn}}{\mysub{\mytyb}{\myb{x}}{\mytmn}}$}
1996 \AxiomC{$\mychkk{\myctx; \myb{x}: \mytya}{\mytmt}{\mytyb}$}
1997 \UnaryInfC{$\mychk{\myabs{\myb{x}}{\mytmt}}{\myfora{\myb{x}}{\mytyb}{\mytyb}}$}
2002 \subsection{Elaboration}
2004 As we mentioned, $\mykant$\ allows the user to define not only values
2005 but also custom data types and records. \emph{Elaboration} consists of
2006 turning these declarations into workable syntax, types, and reduction
2007 rules. The treatment of custom types in $\mykant$\ is heavily inspired
2008 by McBride and McKinna early work on Epigram \citep{McBride2004},
2009 although with some differences.
2011 \subsubsection{Term vectors, telescopes, and assorted notation}
2013 We use a vector notation to refer to a series of term applied to
2014 another, for example $\mytyc{D} \myappsp \vec{A}$ is a shorthand for
2015 $\mytyc{D} \myappsp \mytya_1 \cdots \mytya_n$, for some $n$. $n$ is
2016 consistently used to refer to the length of such vectors, and $i$ to
2017 refer to an index in such vectors. We also often need to `build up'
2018 terms vectors, in which case we use $\myemptyctx$ for an empty vector
2019 and add elements to an existing vector with $\myarg ; \myarg$, similarly
2020 to what we do for context.
2022 To present the elaboration and operations on user defined data types, we
2023 frequently make use what de Bruijn called \emph{telescopes}
2024 \citep{Bruijn91}, a construct that will prove useful when dealing with
2025 the types of type and data constructors. A telescope is a series of
2026 nested typed bindings, such as $(\myb{x} {:} \mynat); (\myb{p} :
2027 \myapp{\myfun{even}}{\myb{x}})$. Consistently with the notation for
2028 contexts and term vectors, we use $\myemptyctx$ to denote an empty
2029 telescope and $\myarg ; \myarg$ to add a new binding to an existing
2032 We refer to telescopes with $\mytele$, $\mytele'$, $\mytele_i$, etc. If
2033 $\mytele$ refers to a telescope, $\mytelee$ refers to the term vector
2034 made up of all the variables bound by $\mytele$. $\mytele \myarr
2035 \mytya$ refers to the type made by turning the telescope into a series
2036 of $\myarr$. Returning to the examples above, we have that
2038 (\myb{x} {:} \mynat); (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat =
2039 (\myb{x} {:} \mynat) \myarr (\myb{p} : \myapp{\myfun{even}}{\myb{x}}) \myarr \mynat
2042 We make use of various operations to manipulate telescopes:
2044 \item $\myhead(\mytele)$ refers to the first type appearing in
2045 $\mytele$: $\myhead((\myb{x} {:} \mynat); (\myb{p} :
2046 \myapp{\myfun{even}}{\myb{x}})) = \mynat$. Similarly,
2047 $\myix_i(\mytele)$ refers to the $i^{th}$ type in a telescope
2049 \item $\mytake_i(\mytele)$ refers to the telescope created by taking the
2050 first $i$ elements of $\mytele$: $\mytake_1((\myb{x} {:} \mynat); (\myb{p} :
2051 \myapp{\myfun{even}}{\myb{x}})) = (\myb{x} {:} \mynat)$
2052 \item $\mytele \vec{A}$ refers to the telescope made by `applying' the
2053 terms in $\vec{A}$ on $\mytele$: $((\myb{x} {:} \mynat); (\myb{p} :
2054 \myapp{\myfun{even}}{\myb{x}}))42 = (\myb{p} :
2055 \myapp{\myfun{even}}{42})$.
2058 \subsubsection{Declarations syntax}
2062 \begin{array}{r@{\ }c@{\ }l}
2063 \mydeclsyn & ::= & \myval{\myb{x}}{\mytmsyn}{\mytmsyn} \\
2064 & | & \mypost{\myb{x}}{\mytmsyn} \\
2065 & | & \myadt{\mytyc{D}}{\mytelesyn}{}{\mydc{c} : \mytelesyn\ |\ \cdots } \\
2066 & | & \myreco{\mytyc{D}}{\mytelesyn}{}{\myfun{f} : \mytmsyn,\ \cdots } \\
2068 \mytelesyn & ::= & \myemptytele \mysynsep \mytelesyn \mycc (\myb{x} {:} \mytmsyn) \\
2069 \mynamesyn & ::= & \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2074 In \mykant\ we have four kind of declarations:
2077 \item[Defined value] A variable, together with a type and a body.
2078 \item[Abstract variable] An abstract variable, with a type but no body.
2079 \item[Inductive data] A datatype, with a type constructor and various data
2080 constructors---somewhat similar to what we find in Haskell. A primitive
2081 recursor (or `destructor') will be generated automatically.
2082 \item[Record] A record, which consists of one data constructor and various
2083 fields, with no recursive occurrences.
2086 Elaborating defined variables consists of type checking body against the
2087 given type, and updating the context to contain the new binding.
2088 Elaborating abstract variables and abstract variables consists of type
2089 checking the type, and updating the context with a new typed variable:
2091 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2093 \AxiomC{$\myjud{\mytmt}{\mytya}$}
2094 \AxiomC{$\myfun{f} \not\in \myctx$}
2096 $\myctx \myelabt \myval{\myfun{f}}{\mytya}{\mytmt} \ \ \myelabf\ \ \myctx; \myfun{f} \mapsto \mytmt : \mytya$
2100 \AxiomC{$\myjud{\mytya}{\mytyp}$}
2101 \AxiomC{$\myfun{f} \not\in \myctx$}
2104 \myctx \myelabt \mypost{\myfun{f}}{\mytya}
2105 \ \ \myelabf\ \ \myctx; \myfun{f} : \mytya
2112 \subsubsection{User defined types}
2115 \begin{subfigure}[b]{\textwidth}
2121 \mynamesyn ::= \cdots \mysynsep \mytyc{D} \mysynsep \mytyc{D}.\mydc{c} \mysynsep \mytyc{D}.\myfun{f}
2126 \mydesc{syntax elaboration:}{\mydeclsyn \myelabf \mytmsyn ::= \cdots}{
2129 \begin{array}{r@{\ }l}
2130 & \myadt{\mytyc{D}}{\mytele}{}{\cdots\ |\ \mydc{c}_n : \mytele_n } \\
2133 \begin{array}{r@{\ }c@{\ }l}
2134 \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \cdots \mysynsep
2135 \mytyc{D}.\mydc{c}_n \myappsp \mytmsyn^{\mytele_n} \mysynsep \mytyc{D}.\myfun{elim} \myappsp \mytmsyn \\
2141 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2146 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2147 \mytyc{D} \not\in \myctx \\
2148 \myinff{\myctx;\ \mytyc{D} : \mytele \myarr \mytyp}{\mytele \mycc \mytele_i \myarr \myapp{\mytyc{D}}{\mytelee}}{\mytyp}\ \ \ (1 \leq i \leq n) \\
2149 \text{For each $(\myb{x} {:} \mytya)$ in each $\mytele_i$, if $\mytyc{D} \in \mytya$, then $\mytya = \myapp{\mytyc{D}}{\vec{\mytmt}}$.}
2153 \begin{array}{r@{\ }c@{\ }l}
2154 \myctx & \myelabt & \myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \\
2155 & & \vspace{-0.2cm} \\
2156 & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\mydc{c}_n : \mytele \mycc \mytele_n \myarr \myapp{\mytyc{D}}{\mytelee}; \\
2158 \begin{array}{@{}r@{\ }l l}
2159 \mytyc{D}.\myfun{elim} : & \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr & \textbf{target} \\
2160 & (\myb{P} {:} \myapp{\mytyc{D}}{\mytelee} \myarr \mytyp) \myarr & \textbf{motive} \\
2164 (\mytele_n \mycc \myhyps(\myb{P}, \mytele_n) \myarr \myapp{\myb{P}}{(\myapp{\mytyc{D}.\mydc{c}_n}{\mytelee_n})}) \myarr
2165 \end{array} \right \}
2166 & \textbf{methods} \\
2167 & \myapp{\myb{P}}{\myb{x}} &
2171 \DisplayProof \\ \vspace{0.2cm}\ \\
2173 \begin{array}{@{}l l@{\ } l@{} r c l}
2174 \textbf{where} & \myhyps(\myb{P}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2175 & \myhyps(\myb{P}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{\mytmt}}) \mycc \mytele &) & \mymetagoes & (\myb{r'} {:} \myapp{\myb{P}}{\myb{r}}) \mycc \myhyps(\myb{P}, \mytele) \\
2176 & \myhyps(\myb{P}, & (\myb{x} {:} \mytya) \mycc \mytele & ) & \mymetagoes & \myhyps(\myb{P}, \mytele)
2182 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2184 $\myadt{\mytyc{D}}{\mytele}{}{ \cdots \ |\ \mydc{c}_n : \mytele_n } \ \ \myelabf$
2185 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2186 \AxiomC{$\mytyc{D}.\mydc{c}_i : \mytele;\mytele_i \myarr \myapp{\mytyc{D}}{\mytelee} \in \myctx$}
2188 \myctx \vdash \myapp{\myapp{\myapp{\mytyc{D}.\myfun{elim}}{(\myapp{\mytyc{D}.\mydc{c}_i}{\vec{\myse{t}}})}}{\myse{P}}}{\vec{\myse{m}}} \myred \myapp{\myapp{\myse{m}_i}{\vec{\mytmt}}}{\myrecs(\myse{P}, \vec{m}, \mytele_i)}
2190 \DisplayProof \\ \vspace{0.2cm}\ \\
2192 \begin{array}{@{}l l@{\ } l@{} r c l}
2193 \textbf{where} & \myrecs(\myse{P}, \vec{m}, & \myemptytele &) & \mymetagoes & \myemptytele \\
2194 & \myrecs(\myse{P}, \vec{m}, & (\myb{r} {:} \myapp{\mytyc{D}}{\vec{A}}); \mytele & ) & \mymetagoes & (\mytyc{D}.\myfun{elim} \myappsp \myb{r} \myappsp \myse{P} \myappsp \vec{m}); \myrecs(\myse{P}, \vec{m}, \mytele) \\
2195 & \myrecs(\myse{P}, \vec{m}, & (\myb{x} {:} \mytya); \mytele &) & \mymetagoes & \myrecs(\myse{P}, \vec{m}, \mytele)
2201 \begin{subfigure}[b]{\textwidth}
2202 \mydesc{syntax elaboration:}{\myelab{\mydeclsyn}{\mytmsyn ::= \cdots}}{
2205 \begin{array}{r@{\ }c@{\ }l}
2206 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2209 \begin{array}{r@{\ }c@{\ }l}
2210 \mytmsyn & ::= & \cdots \mysynsep \myapp{\mytyc{D}}{\mytmsyn^{\mytele}} \mysynsep \mytyc{D}.\mydc{constr} \myappsp \mytmsyn^{n} \mysynsep \cdots \mysynsep \mytyc{D}.\myfun{f}_n \myappsp \mytmsyn \\
2217 \mydesc{context elaboration:}{\myelab{\mydeclsyn}{\myctx}}{
2221 \myinf{\mytele \myarr \mytyp}{\mytyp}\hspace{0.8cm}
2222 \mytyc{D} \not\in \myctx \\
2223 \myinff{\myctx; \mytele; (\myb{f}_j : \myse{F}_j)_{j=1}^{i - 1}}{F_i}{\mytyp} \myind{3} (1 \le i \le n)
2227 \begin{array}{r@{\ }c@{\ }l}
2228 \myctx & \myelabt & \myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \\
2229 & & \vspace{-0.2cm} \\
2230 & \myelabf & \myctx;\ \mytyc{D} : \mytele \myarr \mytyp;\ \cdots;\ \mytyc{D}.\myfun{f}_n : \mytele \myarr (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr \mysub{\myse{F}_n}{\myb{f}_i}{\myapp{\myfun{f}_i}{\myb{x}}}_{i = 1}^{n-1}; \\
2231 & & \mytyc{D}.\mydc{constr} : \mytele \myarr \myse{F}_1 \myarr \cdots \myarr \myse{F}_n \myarr \myapp{\mytyc{D}}{\mytelee};
2237 \mydesc{reduction elaboration:}{\mydeclsyn \myelabf \myctx \vdash \mytmsyn \myred \mytmsyn}{
2239 $\myreco{\mytyc{D}}{\mytele}{}{ \cdots, \myfun{f}_n : \myse{F}_n } \ \ \myelabf$
2240 \AxiomC{$\mytyc{D} \in \myctx$}
2241 \UnaryInfC{$\myctx \vdash \myapp{\mytyc{D}.\myfun{f}_i}{(\mytyc{D}.\mydc{constr} \myappsp \vec{t})} \myred t_i$}
2246 \caption{Elaboration for data types and records.}
2250 Elaborating user defined types is the real effort. First, let's explain
2251 what we can defined, with some examples.
2254 \item[Natural numbers] To define natural numbers, we create a data type
2255 with two constructors: one with zero arguments ($\mydc{zero}$) and one
2256 with one recursive argument ($\mydc{suc}$):
2259 \myadt{\mynat}{ }{ }{
2260 \mydc{zero} \mydcsep \mydc{suc} \myappsp \mynat
2264 This is very similar to what we would write in Haskell:
2265 {\small\[\text{\texttt{data Nat = Zero | Suc Nat}}\]}
2266 Once the data type is defined, $\mykant$\ will generate syntactic
2267 constructs for the type and data constructors, so that we will have
2270 \begin{tabular}{ccc}
2271 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2272 \UnaryInfC{$\myinf{\mynat}{\mytyp}$}
2275 \AxiomC{\phantom{$\mychk{\mytmt}{\mynat}$}}
2276 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{zero}}{\mynat}$}
2279 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2280 \UnaryInfC{$\myinf{\mytyc{\mynat}.\mydc{suc} \myappsp \mytmt}{\mynat}$}
2284 While in Haskell (or indeed in Agda or Coq) data constructors are
2285 treated the same way as functions, in $\mykant$\ they are syntax, so
2286 for example using $\mytyc{\mynat}.\mydc{suc}$ on its own will be a
2287 syntax error. This is necessary so that we can easily infer the type
2288 of polymorphic data constructors, as we will see later.
2290 Moreover, each data constructor is prefixed by the type constructor
2291 name, since we need to retrieve the type constructor of a data
2292 constructor when type checking. This measure aids in the presentation
2293 of various features but it is not needed in the implementation, where
2294 we can have a dictionary to lookup the type constructor corresponding
2295 to each data constructor. When using data constructors in examples I
2296 will omit the type constructor prefix for brevity.
2298 Along with user defined constructors, $\mykant$\ automatically
2299 generates an \emph{eliminator}, or \emph{destructor}, to compute with
2300 natural numbers: If we have $\mytmt : \mynat$, we can destruct
2301 $\mytmt$ using the generated eliminator `$\mynat.\myfun{elim}$':
2304 \AxiomC{$\mychk{\mytmt}{\mynat}$}
2306 \myinf{\mytyc{\mynat}.\myfun{elim} \myappsp \mytmt}{
2308 \myfora{\myb{P}}{\mynat \myarr \mytyp}{ \\ \myapp{\myb{P}}{\mydc{zero}} \myarr (\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}) \myarr \\ \myapp{\myb{P}}{\mytmt}}
2312 $\mynat.\myfun{elim}$ corresponds to the induction principle for
2313 natural numbers: if we have a predicate on numbers ($\myb{P}$), and we
2314 know that predicate holds for the base case
2315 ($\myapp{\myb{P}}{\mydc{zero}}$) and for each inductive step
2316 ($\myfora{\myb{x}}{\mynat}{\myapp{\myb{P}}{\myb{x}} \myarr
2317 \myapp{\myb{P}}{(\myapp{\mydc{suc}}{\myb{x}})}}$), then $\myb{P}$
2318 holds for any number. As with the data constructors, we require the
2319 eliminator to be applied to the `destructed' element.
2321 While the induction principle is usually seen as a mean to prove
2322 properties about numbers, in the intuitionistic setting it is also a
2323 mean to compute. In this specific case we will $\mynat.\myfun{elim}$
2324 will return the base case if the provided number is $\mydc{zero}$, and
2325 recursively apply the inductive step if the number is a
2328 \begin{array}{@{}l@{}l}
2329 \mytyc{\mynat}.\myfun{elim} \myappsp \mydc{zero} & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{pz} \\
2330 \mytyc{\mynat}.\myfun{elim} \myappsp (\mydc{suc} \myappsp \mytmt) & \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps} \myred \myse{ps} \myappsp \mytmt \myappsp (\mynat.\myfun{elim} \myappsp \mytmt \myappsp \myse{P} \myappsp \myse{pz} \myappsp \myse{ps})
2333 The Haskell equivalent would be
2336 \text{\texttt{elim :: Nat -> a -> (Nat -> a -> a) -> a}}\\
2337 \text{\texttt{elim Zero pz ps = pz}}\\
2338 \text{\texttt{elim (Suc n) pz ps = ps n (elim n pz ps)}}
2341 Which buys us the computational behaviour, but not the reasoning power.
2342 % TODO maybe more examples, e.g. Haskell eliminator and fibonacci
2344 \item[Binary trees] Now for a polymorphic data type: binary trees, since
2345 lists are too similar to natural numbers to be interesting.
2348 \myadt{\mytree}{\myappsp (\myb{A} {:} \mytyp)}{ }{
2349 \mydc{leaf} \mydcsep \mydc{node} \myappsp (\myapp{\mytree}{\myb{A}}) \myappsp \myb{A} \myappsp (\myapp{\mytree}{\myb{A}})
2353 Now the purpose of constructors as syntax can be explained: what would
2354 the type of $\mydc{leaf}$ be? If we were to treat it as a `normal'
2355 term, we would have to specify the type parameter of the tree each
2356 time the constructor is applied:
2358 \begin{array}{@{}l@{\ }l}
2359 \mydc{leaf} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}}} \\
2360 \mydc{node} & : \myfora{\myb{A}}{\mytyp}{\myapp{\mytree}{\myb{A}} \myarr \myb{A} \myarr \myapp{\mytree}{\myb{A}} \myarr \myapp{\mytree}{\myb{A}}}
2363 The problem with this approach is that creating terms is incredibly
2364 verbose and dull, since we would need to specify the type parameters
2365 each time. For example if we wished to create a $\mytree \myappsp
2366 \mynat$ with two nodes and three leaves, we would have to write
2368 \mydc{node} \myappsp \mynat \myappsp (\mydc{node} \myappsp \mynat \myappsp (\mydc{leaf} \myappsp \mynat) \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp (\mydc{leaf} \myappsp \mynat)) \myappsp \mydc{zero} \myappsp (\mydc{leaf} \myappsp \mynat)
2370 The redundancy of $\mynat$s is quite irritating. Instead, if we treat
2371 constructors as syntactic elements, we can `extract' the type of the
2372 parameter from the type that the term gets checked against, much like
2373 we get the type of abstraction arguments:
2377 \AxiomC{$\mychk{\mytya}{\mytyp}$}
2378 \UnaryInfC{$\mychk{\mydc{leaf}}{\myapp{\mytree}{\mytya}}$}
2381 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2382 \AxiomC{$\mychk{\mytmt}{\mytya}$}
2383 \AxiomC{$\mychk{\mytmm}{\mytree \myappsp \mytya}$}
2384 \TrinaryInfC{$\mychk{\mydc{node} \myappsp \mytmm \myappsp \mytmt \myappsp \mytmn}{\mytree \myappsp \mytya}$}
2388 Which enables us to write, much more concisely
2390 \mydc{node} \myappsp (\mydc{node} \myappsp \mydc{leaf} \myappsp (\myapp{\mydc{suc}}{\mydc{zero}}) \myappsp \mydc{leaf}) \myappsp \mydc{zero} \myappsp \mydc{leaf} : \myapp{\mytree}{\mynat}
2392 We gain an annotation, but we lose the myriad of types applied to the
2393 constructors. Conversely, with the eliminator for $\mytree$, we can
2394 infer the type of the arguments given the type of the destructed:
2397 \AxiomC{$\myinf{\mytmt}{\myapp{\mytree}{\mytya}}$}
2399 \myinf{\mytree.\myfun{elim} \myappsp \mytmt}{
2401 (\myb{P} {:} \myapp{\mytree}{\mytya} \myarr \mytyp) \myarr \\
2402 \myapp{\myb{P}}{\mydc{leaf}} \myarr \\
2403 ((\myb{l} {:} \myapp{\mytree}{\mytya}) (\myb{x} {:} \mytya) (\myb{r} {:} \myapp{\mytree}{\mytya}) \myarr \myapp{\myb{P}}{\myb{l}} \myarr
2404 \myapp{\myb{P}}{\myb{r}} \myarr \myb{P} \myappsp (\mydc{node} \myappsp \myb{l} \myappsp \myb{x} \myappsp \myb{r})) \myarr \\
2405 \myapp{\myb{P}}{\mytmt}
2410 As expected, the eliminator embodies structural induction on trees.
2412 \item[Empty type] We have presented types that have at least one
2413 constructors, but nothing prevents us from defining types with
2414 \emph{no} constructors:
2416 \myadt{\mytyc{Empty}}{ }{ }{ }
2418 What shall the `induction principle' on $\mytyc{Empty}$ be? Does it
2419 even make sense to talk about induction on $\mytyc{Empty}$?
2420 $\mykant$\ does not care, and generates an eliminator with no `cases',
2421 and thus corresponding to the $\myfun{absurd}$ that we know and love:
2424 \AxiomC{$\myinf{\mytmt}{\mytyc{Empty}}$}
2425 \UnaryInfC{$\myinf{\myempty.\myfun{elim} \myappsp \mytmt}{(\myb{P} {:} \mytmt \myarr \mytyp) \myarr \myapp{\myb{P}}{\mytmt}}$}
2428 \item[Ordered lists] Up to this point, the examples shown are nothing
2429 new to the \{Haskell, SML, OCaml, functional\} programmer. However
2430 dependent types let us express much more than
2433 \item[Dependent products] Apart from $\mysyn{data}$, $\mykant$\ offers
2434 us another way to define types: $\mysyn{record}$. A record is a
2435 datatype with one constructor and `projections' to extract specific
2436 fields of the said constructor.
2438 For example, we can recover dependent products:
2441 \myreco{\mytyc{Prod}}{\myappsp (\myb{A} {:} \mytyp) \myappsp (\myb{B} {:} \myb{A} \myarr \mytyp)}{\\ \myind{2}}{\myfst : \myb{A}, \mysnd : \myapp{\myb{B}}{\myb{fst}}}
2444 Here $\myfst$ and $\mysnd$ are the projections, with their respective
2445 types. Note that each field can refer to the preceding fields. A
2446 constructor will be automatically generated, under the name of
2447 $\mytyc{Prod}.\mydc{constr}$. Dually to data types, we will omit the
2448 type constructor prefix for record projections.
2450 Following the bidirectionality of the system, we have that projections
2451 (the destructors of the record) infer the type, while the constructor
2456 \AxiomC{$\mychk{\mytmm}{\mytya}$}
2457 \AxiomC{$\mychk{\mytmn}{\myapp{\mytyb}{\mytmm}}$}
2458 \BinaryInfC{$\mychk{\mytyc{Prod}.\mydc{constr} \myappsp \mytmm \myappsp \mytmn}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2460 \UnaryInfC{\phantom{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}}
2463 \AxiomC{$\myinf{\mytmt}{\mytyc{Prod} \myappsp \mytya \myappsp \mytyb}$}
2464 \UnaryInfC{$\myinf{\myfun{fst} \myappsp \mytmt}{\mytya}$}
2466 \UnaryInfC{$\myinf{\myfun{snd} \myappsp \mytmt}{\mytyb \myappsp (\myfst \myappsp \mytmt)}$}
2470 What we have is equivalent to ITT's dependent products.
2473 Following the intuition given by the examples, the mechanised
2474 elaboration is presented in figure \ref{fig:elab}, which is essentially
2475 a modification of figure 9 of \citep{McBride2004}\footnote{However, our
2476 datatypes do not have indices, we do bidirectional typechecking by
2477 treating constructors/destructors as syntactic constructs, and we have
2480 In data types declarations we allow recursive occurrences as long as
2481 they are \emph{strictly positive}, employing a syntactic check to make
2482 sure that this is the case. See \cite{Dybjer1991} for a more formal
2483 treatment of inductive definitions in ITT.
2485 For what concerns records, recursive occurrences are disallowed. The
2486 reason for this choice is answered by the reason for the choice of
2487 having records at all: we need records to give the user types with
2488 $\eta$-laws for equality, as we saw in section % TODO add section
2489 and in the treatment of OTT in section \ref{sec:ott}. If we tried to
2490 $\eta$-expand recursive data types, we would expand forever.
2492 To implement bidirectional type checking for constructors and
2493 destructors, we store their types in full in the context, and then
2494 instantiate when due:
2496 \mydesc{typing:}{ }{
2499 \mytyc{D} : \mytele \myarr \mytyp \in \myctx \hspace{1cm}
2500 \mytyc{D}.\mydc{c} : \mytele \mycc \mytele' \myarr
2501 \myapp{\mytyc{D}}{\mytelee} \in \myctx \\
2502 \mytele'' = (\mytele;\mytele')\vec{A} \hspace{1cm}
2503 \mychkk{\myctx; \mytake_{i-1}(\mytele'')}{t_i}{\myix_i( \mytele'')}\ \
2504 (1 \le i \le \mytele'')
2507 \UnaryInfC{$\mychk{\myapp{\mytyc{D}.\mydc{c}}{\vec{t}}}{\myapp{\mytyc{D}}{\vec{A}}}$}
2512 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2513 \AxiomC{$\mytyc{D}.\myfun{f} : \mytele \mycc (\myb{x} {:}
2514 \myapp{\mytyc{D}}{\mytelee}) \myarr \myse{F}$}
2515 \AxiomC{$\myjud{\mytmt}{\myapp{\mytyc{D}}{\vec{A}}}$}
2516 \TrinaryInfC{$\myinf{\myapp{\mytyc{D}.\myfun{f}}{\mytmt}}{(\mytele
2517 \mycc (\myb{x} {:} \myapp{\mytyc{D}}{\mytelee}) \myarr
2518 \myse{F})(\vec{A};\mytmt)}$}
2522 \subsubsection{Why user defined types?}
2524 % TODO reference levitated theories, indexed containers
2528 \subsection{Cumulative hierarchy and typical ambiguity}
2529 \label{sec:term-hierarchy}
2531 A type hierarchy as presented in section \label{sec:itt} is a
2532 considerable burden on the user, on various levels. Consider for
2533 example how we recovered disjunctions in section \ref{sec:disju}: we
2534 have a function that takes two $\mytyp_0$ and forms a new $\mytyp_0$.
2535 What if we wanted to form a disjunction containing two $\mytyp_0$, or
2536 $\mytyp_{42}$? Our definition would fail us, since $\mytyp_0 :
2539 One way to solve this issue is a \emph{cumulative} hierarchy, where
2540 $\mytyp_{l_1} : \mytyp_{l_2}$ iff $l_1 < l_2$. This way we retain
2541 consistency, while allowing for `large' definitions that work on small
2542 types too. For example we might define our disjunction to be
2544 \myarg\myfun{$\vee$}\myarg : \mytyp_{100} \myarr \mytyp_{100} \myarr \mytyp_{100}
2546 And hope that $\mytyp_{100}$ will be large enough to fit all the types
2547 that we want to use with our disjunction. However, there are two
2548 problems with this. First, there is the obvious clumsyness of having to
2549 manually specify the size of types. More importantly, if we want to use
2550 $\myfun{$\vee$}$ itself as an argument to other type-formers, we need to
2551 make sure that those allow for types at least as large as
2554 A better option is to employ a mechanised version of what Russell called
2555 \emph{typical ambiguity}: we let the user live under the illusion that
2556 $\mytyp : \mytyp$, but check that the statements about types are
2557 consistent behind the hood. $\mykant$\ implements this following the
2558 lines of \cite{Huet1988}. See also \citep{Harper1991} for a published
2559 reference, although describing a more complex system allowing for both
2560 explicit and explicit hierarchy at the same time.
2562 We define a partial ordering on the levels, with both weak ($\le$) and
2563 strong ($<$) constraints---the laws governing them being the same as the
2564 ones governing $<$ and $\le$ for the natural numbers. Each occurrence
2565 of $\mytyp$ is decorated with a unique reference, and we keep a set of
2566 constraints and add new constraints as we type check, generating new
2567 references when needed.
2569 For example, when type checking the type $\mytyp\, r_1$, where $r_1$
2570 denotes the unique reference assigned to that term, we will generate a
2571 new fresh reference $\mytyp\, r_2$, and add the constraint $r_1 < r_2$
2572 to the set. When type checking $\myctx \vdash
2573 \myfora{\myb{x}}{\mytya}{\mytyb}$, if $\myctx \vdash \mytya : \mytyp\,
2574 r_1$ and $\myctx; \myb{x} : \mytyb \vdash \mytyb : \mytyp\,r_2$; we will
2575 generate new reference $r$ and add $r_1 \le r$ and $r_2 \le r$ to the
2578 If at any point the constraint set becomes inconsistent, type checking
2579 fails. Moreover, when comparing two $\mytyp$ terms we equate their
2580 respective references with two $\le$ constraints---the details are
2581 explained in section \ref{sec:hier-impl}.
2583 Another more flexible but also more verbose alternative is the one
2584 chosen by Agda, where levels can be quantified so that the relationship
2585 between arguments and result in type formers can be explicitly
2588 \myarg\myfun{$\vee$}\myarg : (l_1\, l_2 : \mytyc{Level}) \myarr \mytyp_{l_1} \myarr \mytyp_{l_2} \myarr \mytyp_{l_1 \mylub l_2}
2590 Inference algorithms to automatically derive this kind of relationship
2591 are currently subject of research. We chose less flexible but more
2592 concise way, since it is easier to implement and better understood.
2594 \subsection{Observational equality, \mykant\ style}
2596 There are two correlated differences between $\mykant$\ and the theory
2597 used to present OTT. The first is that in $\mykant$ we have a type
2598 hierarchy, which lets us, for example, abstract over types. The second
2599 is that we let the user define inductive types.
2601 Reconciling propositions for OTT and a hierarchy had already been
2602 investigated by Conor McBride\footnote{See
2603 \url{http://www.e-pig.org/epilogue/index.html?p=1098.html}.}, and we
2604 follow his footsteps. Most of the work, as an extension of elaboration,
2605 is to generate reduction rules and coercions.
2607 \subsubsection{The \mykant\ prelude, and $\myprop$ositions}
2609 Before defining $\myprop$, we define some basic types inside $\mykant$,
2610 as the target for the $\myprop$ decoder:
2616 \myadt{\mytyc{Empty}}{}{ }{ } \\
2617 \myfun{absurd} : (\myb{A} {:} \mytyp) \myarr \mytyc{Empty} \myarr \myb{A} \mapsto \\
2618 \myind{2} \myabs{\myb{A\ \myb{bot}}}{\mytyc{Empty}.\myfun{elim} \myappsp \myb{bot} \myappsp (\myabs{\_}{\myb{A}})} \\
2621 \myreco{\mytyc{Unit}}{}{\mydc{tt}}{ } \\ \ \\
2623 \myreco{\mytyc{Prod}}{\myappsp (\myb{A}\ \myb{B} {:} \mytyp)}{ }{\myfun{fst} : \myb{A}, \myfun{snd} : \myb{B} }
2627 When using $\mytyc{Prod}$, we shall use $\myprod$ to define `nested'
2628 products, and $\myproj{n}$ to project elements from them, so that
2632 \mytya \myprod \mytyb = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myunit) \\
2633 \mytya \myprod \mytyb \myprod \myse{C} = \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp (\mytyc{Prod} \myappsp \mytyc \myappsp \myunit)) \\
2635 \myproj{1} : \mytyc{Prod} \myappsp \mytya \myappsp \mytyb \myarr \mytya \\
2636 \myproj{2} : \mytyc{Prod} \myappsp \mytya \myappsp (\mytyc{Prod} \myappsp \mytyb \myappsp \myse{C}) \myarr \mytyb \\
2641 And so on, so that $\myproj{n}$ will work with all products with at
2642 least than $n$ elements. Then we can define propositions, and decoding:
2646 \begin{array}{r@{\ }c@{\ }l}
2647 \mytmsyn & ::= & \cdots \mysynsep \myprdec{\myprsyn} \\
2648 \myprsyn & ::= & \mybot \mysynsep \mytop \mysynsep \myprsyn \myand \myprsyn \mysynsep \myprfora{\myb{x}}{\mytmsyn}{\myprsyn}
2653 \mydesc{proposition decoding:}{\myprdec{\mytmsyn} \myred \mytmsyn}{
2656 \begin{array}{l@{\ }c@{\ }l}
2657 \myprdec{\mybot} & \myred & \myempty \\
2658 \myprdec{\mytop} & \myred & \myunit
2663 \begin{array}{r@{ }c@{ }l@{\ }c@{\ }l}
2664 \myprdec{&\myse{P} \myand \myse{Q} &} & \myred & \myprdec{\myse{P}} \myprod \myprdec{\myse{Q}} \\
2665 \myprdec{&\myprfora{\myb{x}}{\mytya}{\myse{P}} &} & \myred &
2666 \myfora{\myb{x}}{\mytya}{\myprdec{\myse{P}}}
2672 \subsubsection{Why $\myprop$?}
2674 It is worth to ask if $\myprop$ is needed at all. It is perfectly
2675 possible to have the type checker identify propositional types
2676 automatically, and in fact that is what The author initially planned to
2677 identify the propositional fragment iinternally \cite{Jacobs1994}.
2679 \subsubsection{OTT constructs}
2683 \begin{array}{r@{\ }c@{\ }l}
2684 \mytmsyn & ::= & \cdots \mysynsep \mycoee{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \mysynsep
2685 \mycohh{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2686 \myprsyn & ::= & \cdots \mysynsep \myjm{\mytmsyn}{\mytmsyn}{\mytmsyn}{\mytmsyn} \\
2691 \mydesc{equality reduction:}{\myctx \vdash \myprsyn \myred \myprsyn}{
2695 \begin{array}{r@{\ }l}
2697 \myjm{\myfora{\myb{x_1}}{\mytya_1}{\mytyb_1}}{\mytyp}{\myfora{\myb{x_2}}{\mytya_2}{\mytyb_2}}{\mytyp} \myred \\
2698 & \myind{2} \mytya_2 \myeq \mytya_1 \myand \\
2699 & \myind{2} \myprfora{\myb{x_2}}{\mytya_2}{\myprfora{\myb{x_1}}{\mytya_1}{
2700 \myjm{\myb{x_2}}{\mytya_2}{\myb{x_1}}{\mytya_1} \myimpl \mytyb_1 \myeq \mytyb_2
2708 \AxiomC{$\mytyc{D} : \mytele \myarr \mytyp \in \myctx$}
2710 \begin{array}{r@{\ }l}
2712 \myjm{\mytyc{D} \myappsp \vec{A}}{\mytyp}{\mytyc{D} \myappsp \vec{B}}{\mytyp} \myred \\
2713 & \myind{2} \myjm{\mytya_1}{\myhead(\mytele)}{\mytyb_1}{\myhead(\mytele)} \myand \cdots \myand \\
2714 & \myind{2} \myjm{\mytya_n}{\myhead(\mytele(A_1 \cdots A_{n-1}))}{\mytyb_n}{\myhead(\mytele(B_1 \cdots B_{n-1}))}
2722 \UnaryInfC{$\myctx \vdash \myjm{\mytyp}{\mytyp}{\mytyp}{\mytyp} \myred \mytop$}
2729 \mydataty(\mytyc{D}, \myctx)\hspace{0.8cm}
2730 \mytyc{D}.\mydc{c}_i : \mytele;\mytele' \myarr \mytyc{D} \myappsp \mytelee \in \myctx \\
2731 \mytele_A = (\mytele;\mytele')\vec{A}\hspace{0.8cm}
2732 \mytele_B = (\mytele;\mytele')\vec{B}
2737 \myctx \vdash \myjm{\mytyc{D}.\mydc{c}_i \myappsp \vec{\mytmm}}{\mytyc{D} \myappsp \vec{A}}{\mytyc{D}.\mydc{c}_i \myappsp \vec{\mytmn}}{\mytyc{D} \myappsp \vec{B}} \myred \\
2738 \myind{2} \myjm{\mytmm_1}{\myhead(\mytele_A)}{\mytmn_1}{\myhead(\mytele_B)} \myand \cdots \myand \\
2739 \myind{2} \myjm{\mytmm_n}{\mytya_n}{\mytmn_n}{\mytyb_n}
2746 \AxiomC{$\myisreco(\mytyc{D}, \myctx)$}
2747 \UnaryInfC{$\myctx \vdash \myjm{\mytmm}{\mytyc{D} \myappsp \vec{A}}{\mytmn}{\mytyc{D} \myappsp \vec{B}} \myred foo$}
2752 \UnaryInfC{$\mytya \myeq \mytyb \myred \mybot\ \text{if $\mytya$ and $\mytyb$ are canonical types.}$}
2756 \subsubsection{$\myprop$ and the hierarchy}
2758 Where is $\myprop$ placed in the $\mytyp$ hierarchy?
2760 \subsubsection{Quotation and irrelevance}
2765 \section{\mykant : The practice}
2766 \label{sec:kant-practice}
2768 The codebase consists of around 2500 lines of Haskell, as reported by
2769 the \texttt{cloc} utility. The high level design is inspired by Conor
2770 McBride's work on various incarnations of Epigram, and specifically by
2771 the first version as described \citep{McBride2004} and the codebase for
2772 the new version \footnote{Available intermittently as a \texttt{darcs}
2773 repository at \url{http://sneezy.cs.nott.ac.uk/darcs/Pig09}.}. In
2774 many ways \mykant\ is something in between the first and second version
2777 The interaction happens in a read-eval-print loop (REPL). The REPL is a
2778 available both as a commandline application and in a web interface,
2779 which is available at \url{kant.mazzo.li} and presents itself as in
2780 figure \ref{fig:kant-web}.
2784 \includegraphics[scale=1.0]{kant-web.png}
2786 \caption{The \mykant\ web prompt.}
2787 \label{fig:kant-web}
2790 The interaction with the user takes place in a loop living in and updating a
2791 context \mykant\ declarations. The user inputs a new declaration that goes
2792 through various stages starts with the user inputing a \mykant\ declaration or
2793 another REPL command, which then goes through various stages that can end up
2794 in a context update, or in failures of various kind. The process is described
2795 diagrammatically in figure \ref{fig:kant-process}:
2798 \item[Parse] In this phase the text input gets converted to a sugared
2799 version of the core language.
2801 \item[Desugar] The sugared declaration is converted to a core term.
2803 \item[Reference] Occurrences of $\mytyp$ get decorated by a unique reference,
2804 which is necessary to implement the type hierarchy check.
2806 \item[Elaborate] Convert the declaration to some context item, which might be
2807 a value declaration (type and body) or a data type declaration (constructors
2808 and destructors). This phase works in tandem with \textbf{Typechecking},
2809 which in turns needs to \textbf{Evaluate} terms.
2811 \item[Distill] and report the result. `Distilling' refers to the process of
2812 converting a core term back to a sugared version that the user can
2813 visualise. This can be necessary both to display errors including terms or
2814 to display result of evaluations or type checking that the user has
2817 \item[Pretty print] Format the terms in a nice way, and display the result to
2824 \tikzstyle{block} = [rectangle, draw, text width=5em, text centered, rounded
2825 corners, minimum height=2.5em, node distance=0.7cm]
2827 \tikzstyle{decision} = [diamond, draw, text width=4.5em, text badly
2828 centered, inner sep=0pt, node distance=0.7cm]
2830 \tikzstyle{line} = [draw, -latex']
2832 \tikzstyle{cloud} = [draw, ellipse, minimum height=2em, text width=5em, text
2833 centered, node distance=1.5cm]
2836 \begin{tikzpicture}[auto]
2837 \node [cloud] (user) {User};
2838 \node [block, below left=1cm and 0.1cm of user] (parse) {Parse};
2839 \node [block, below=of parse] (desugar) {Desugar};
2840 \node [block, below=of desugar] (reference) {Reference};
2841 \node [block, below=of reference] (elaborate) {Elaborate};
2842 \node [block, left=of elaborate] (tycheck) {Typecheck};
2843 \node [block, left=of tycheck] (evaluate) {Evaluate};
2844 \node [decision, right=of elaborate] (error) {Error?};
2845 \node [block, right=of parse] (distill) {Distill};
2846 \node [block, right=of desugar] (update) {Update context};
2848 \path [line] (user) -- (parse);
2849 \path [line] (parse) -- (desugar);
2850 \path [line] (desugar) -- (reference);
2851 \path [line] (reference) -- (elaborate);
2852 \path [line] (elaborate) edge[bend right] (tycheck);
2853 \path [line] (tycheck) edge[bend right] (elaborate);
2854 \path [line] (elaborate) -- (error);
2855 \path [line] (error) edge[out=0,in=0] node [near start] {yes} (distill);
2856 \path [line] (error) -- node [near start] {no} (update);
2857 \path [line] (update) -- (distill);
2858 \path [line] (distill) -- (user);
2859 \path [line] (tycheck) edge[bend right] (evaluate);
2860 \path [line] (evaluate) edge[bend right] (tycheck);
2863 \caption{High level overview of the life of a \mykant\ prompt cycle.}
2864 \label{fig:kant-process}
2867 \subsection{Parsing and Sugar}
2869 \subsection{Term representation and context}
2870 \label{sec:term-repr}
2872 \subsection{Type checking}
2874 \subsection{Type hierarchy}
2875 \label{sec:hier-impl}
2877 \subsection{Elaboration}
2879 \section{Evaluation}
2881 \section{Future work}
2883 % TODO coinduction (obscoin, gimenez), pattern unification (miller,
2884 % gundry), partiality monad (NAD)
2888 \section{Notation and syntax}
2890 Syntax, derivation rules, and reduction rules, are enclosed in frames describing
2891 the type of relation being established and the syntactic elements appearing,
2894 \mydesc{typing:}{\myjud{\mytmsyn}{\mytysyn}}{
2895 Typing derivations here.
2898 In the languages presented and Agda code samples I also highlight the syntax,
2899 following a uniform color and font convention:
2902 \begin{tabular}{c | l}
2903 $\mytyc{Sans}$ & Type constructors. \\
2904 $\mydc{sans}$ & Data constructors. \\
2905 % $\myfld{sans}$ & Field accessors (e.g. \myfld{fst} and \myfld{snd} for products). \\
2906 $\mysyn{roman}$ & Keywords of the language. \\
2907 $\myfun{roman}$ & Defined values and destructors. \\
2908 $\myb{math}$ & Bound variables.
2912 Moreover, I will from time to time give examples in the Haskell programming
2913 language as defined in \citep{Haskell2010}, which I will typeset in
2914 \texttt{teletype} font. I assume that the reader is already familiar with
2915 Haskell, plenty of good introductions are available \citep{LYAH,ProgInHask}.
2917 When presenting grammars, I will use a word in $\mysynel{math}$ font
2918 (e.g. $\mytmsyn$ or $\mytysyn$) to indicate indicate nonterminals. Additionally,
2919 I will use quite flexibly a $\mysynel{math}$ font to indicate a syntactic
2920 element. More specifically, terms are usually indicated by lowercase letters
2921 (often $\mytmt$, $\mytmm$, or $\mytmn$); and types by an uppercase letter (often
2922 $\mytya$, $\mytyb$, or $\mytycc$).
2924 When presenting type derivations, I will often abbreviate and present multiple
2925 conclusions, each on a separate line:
2927 \AxiomC{$\myjud{\mytmt}{\mytya \myprod \mytyb}$}
2928 \UnaryInfC{$\myjud{\myapp{\myfst}{\mytmt}}{\mytya}$}
2930 \UnaryInfC{$\myjud{\myapp{\mysnd}{\mytmt}}{\mytyb}$}
2933 I will often present `definition' in the described calculi and in
2934 $\mykant$\ itself, like so:
2937 \myfun{name} : \mytysyn \\
2938 \myfun{name} \myappsp \myb{arg_1} \myappsp \myb{arg_2} \myappsp \cdots \mapsto \mytmsyn
2941 To define operators, I use a mixfix notation similar
2942 to Agda, where $\myarg$s denote arguments, for example
2945 \myarg \mathrel{\myfun{$\wedge$}} \myarg : \mybool \myarr \mybool \myarr \mybool \\
2946 \myb{b_1} \mathrel{\myfun{$\wedge$}} \myb{b_2} \mapsto \cdots
2952 \subsection{ITT renditions}
2953 \label{app:itt-code}
2955 \subsubsection{Agda}
2956 \label{app:agda-itt}
2958 Note that in what follows rules for `base' types are
2959 universe-polymorphic, to reflect the exposition. Derived definitions,
2960 on the other hand, mostly work with \mytyc{Set}, reflecting the fact
2961 that in the theory presented we don't have universe polymorphism.
2967 data Empty : Set where
2969 absurd : ∀ {a} {A : Set a} → Empty → A
2972 ¬_ : ∀ {a} → (A : Set a) → Set a
2975 record Unit : Set where
2978 record _×_ {a b} (A : Set a) (B : A → Set b) : Set (a ⊔ b) where
2985 data Bool : Set where
2988 if_/_then_else_ : ∀ {a} (x : Bool) (P : Bool → Set a) → P true → P false → P x
2989 if true / _ then x else _ = x
2990 if false / _ then _ else x = x
2992 if_then_else_ : ∀ {a} (x : Bool) {P : Bool → Set a} → P true → P false → P x
2993 if_then_else_ x {P} = if_/_then_else_ x P
2995 data W {s p} (S : Set s) (P : S → Set p) : Set (s ⊔ p) where
2996 _◁_ : (s : S) → (P s → W S P) → W S P
2998 rec : ∀ {a b} {S : Set a} {P : S → Set b}
2999 (C : W S P → Set) → -- some conclusion we hope holds
3000 ((s : S) → -- given a shape...
3001 (f : P s → W S P) → -- ...and a bunch of kids...
3002 ((p : P s) → C (f p)) → -- ...and C for each kid in the bunch...
3003 C (s ◁ f)) → -- ...does C hold for the node?
3004 (x : W S P) → -- If so, ...
3005 C x -- ...C always holds.
3006 rec C c (s ◁ f) = c s f (λ p → rec C c (f p))
3008 module Examples-→ where
3015 -- These pragmas are needed so we can use number literals.
3016 {-# BUILTIN NATURAL ℕ #-}
3017 {-# BUILTIN ZERO zero #-}
3018 {-# BUILTIN SUC suc #-}
3020 data List (A : Set) : Set where
3022 _∷_ : A → List A → List A
3024 length : ∀ {A} → List A → ℕ
3026 length (_ ∷ l) = suc (length l)
3031 suc x > suc y = x > y
3033 head : ∀ {A} → (l : List A) → length l > 0 → A
3034 head [] p = absurd p
3037 module Examples-× where
3043 even (suc zero) = Empty
3044 even (suc (suc n)) = even n
3049 5-not-even : ¬ (even 5)
3052 there-is-an-even-number : ℕ × even
3053 there-is-an-even-number = 6 , 6-even
3055 _∨_ : (A B : Set) → Set
3056 A ∨ B = Bool × (λ b → if b then A else B)
3058 left : ∀ {A B} → A → A ∨ B
3061 right : ∀ {A B} → B → A ∨ B
3064 [_,_] : {A B C : Set} → (A → C) → (B → C) → A ∨ B → C
3066 (if (fst x) / (λ b → if b then _ else _ → _) then f else g) (snd x)
3068 module Examples-W where
3073 Tr b = if b then Unit else Empty
3079 zero = false ◁ absurd
3082 suc n = true ◁ (λ _ → n)
3088 if b / (λ b → (Tr b → ℕ) → (Tr b → ℕ) → ℕ)
3089 then (λ _ f → (suc (f tt))) else (λ _ _ → y))
3092 List : (A : Set) → Set
3093 List A = W (A ∨ Unit) (λ s → Tr (fst s))
3096 [] = (false , tt) ◁ absurd
3098 _∷_ : ∀ {A} → A → List A → List A
3099 x ∷ l = (true , x) ◁ (λ _ → l)
3101 _++_ : ∀ {A} → List A → List A → List A
3103 (λ _ → List _ → List _)
3107 module Equality where
3110 data _≡_ {a} {A : Set a} : A → A → Set a where
3113 ≡-elim : ∀ {a b} {A : Set a}
3114 (P : (x y : A) → x ≡ y → Set b) →
3115 ∀ {x y} → P x x (refl x) → (x≡y : x ≡ y) → P x y x≡y
3116 ≡-elim P p (refl x) = p
3118 subst : ∀ {A : Set} (P : A → Set) → ∀ {x y} → (x≡y : x ≡ y) → P x → P y
3119 subst P x≡y p = ≡-elim (λ _ y _ → P y) p x≡y
3121 sym : ∀ {A : Set} (x y : A) → x ≡ y → y ≡ x
3122 sym x y p = subst (λ y′ → y′ ≡ x) p (refl x)
3124 trans : ∀ {A : Set} (x y z : A) → x ≡ y → y ≡ z → x ≡ z
3125 trans x y z p q = subst (λ z′ → x ≡ z′) q p
3127 cong : ∀ {A B : Set} (x y : A) → x ≡ y → (f : A → B) → f x ≡ f y
3128 cong x y p f = subst (λ z → f x ≡ f z) p (refl (f x))
3131 \subsubsection{\mykant}
3133 The following things are missing: $\mytyc{W}$-types, since our
3134 positivity check is overly strict, and equality, since we haven't
3135 implemented that yet.
3138 \verbatiminput{itt.ka}
3141 \subsection{\mykant\ examples}
3144 \verbatiminput{examples.ka}
3147 \subsection{\mykant's hierachy}
3149 This rendition of the Hurken's paradox does not type check with the
3150 hierachy enabled, type checks and loops without it. Adapted from an
3151 Agda version, available at
3152 \url{http://code.haskell.org/Agda/test/succeed/Hurkens.agda}.
3155 \verbatiminput{hurkens.ka}
3158 \bibliographystyle{authordate1}
3159 \bibliography{thesis}