more stuff
[bitonic-mengthesis.git] / docs / background.tex
1 \documentclass[article, a4paper]{article}
2 \usepackage[T1]{fontenc}
3 \usepackage{fixltx2e}
4 \usepackage{enumitem}
5 \usepackage[normalem]{ulem}
6 \usepackage{graphicx}
7 \usepackage{subcaption}
8
9 %% -----------------------------------------------------------------------------
10 %% Fonts
11 %% \usepackage[osf]{libertine}
12 %% \usepackage{helvet}
13 %% \usepackage{lmodern}
14 %% \IfFileExists{microtype.sty}{\usepackage{microtype}}{}
15
16 %% -----------------------------------------------------------------------------
17 %% Diagrams
18 % \usepackage{tikz}
19 % \usetikzlibrary{positioning}
20 %% \usetikzlibrary{shapes}
21 %% \usetikzlibrary{arrows}
22 %% \usepackage{tikz-cd}
23 %% \usepackage{pgfplots}
24 \usepackage[all]{xy}
25
26 %% -----------------------------------------------------------------------------
27 %% Symbols
28 \usepackage{amssymb,amsmath}
29 \usepackage{wasysym}
30 \usepackage{turnstile}
31 \usepackage{centernot}
32
33 %% -----------------------------------------------------------------------------
34 %% Utils
35 \usepackage{bussproofs}
36 \usepackage{umoline}
37 \usepackage[export]{adjustbox}
38 \usepackage{multicol}
39
40 %% -----------------------------------------------------------------------------
41 %% Bibtex
42 \usepackage{natbib}
43
44 %% Numbering depth
45 %% \setcounter{secnumdepth}{0}
46
47 %% -----------------------------------------------------------------------------
48 % We will generate all images so they have a width \maxwidth. This means
49 % that they will get their normal width if they fit onto the page, but
50 % are scaled down if they would overflow the margins.
51 \makeatletter
52 \def\maxwidth{
53   \ifdim\Gin@nat@width>\linewidth\linewidth
54   \else\Gin@nat@width\fi
55 }
56 \makeatother
57
58 %% -----------------------------------------------------------------------------
59 %% Links
60 \usepackage[
61   setpagesize=false, % page size defined by xetex
62   unicode=false, % unicode breaks when used with xetex
63   xetex
64 ]{hyperref}
65
66 \hypersetup{
67   breaklinks=true,
68   bookmarks=true,
69   pdfauthor={Francesco Mazzoli <fm2209@ic.ac.uk>},
70   pdftitle={The Paths Towards Observational Equality},
71   colorlinks=false,
72   pdfborder={0 0 0}
73 }
74
75 % Make links footnotes instead of hotlinks:
76 % \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
77
78 % avoid problems with \sout in headers with hyperref:
79 \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
80
81 \title{The Paths Towards Observational Equality}
82 \author{Francesco Mazzoli \href{mailto:fm2209@ic.ac.uk}{\nolinkurl{<fm2209@ic.ac.uk>}}}
83 \date{December 2012}
84
85 \begin{document}
86
87 \maketitle
88
89 \setlength{\tabcolsep}{12pt}
90
91 The marriage between programming and logic has been a very fertile one.  In
92 particular, since the simply typed lambda calculus (STLC), a number of type
93 systems have been devised with increasing expressive power.
94
95 In the next sections I will give a very brief overview of STLC, and then
96 describe how to augment it to reach the theory I am interested in,
97 Inutitionistic Type Theory (ITT), also known as Martin-L\"{o}f Type Theory after
98 its inventor.  The exposition is quite dense since there is a lot of material to
99 cover, for a more complete treatment of the material the reader can refer to
100 \citep{Thompson1991, Pierce2002}.
101
102 I will then explain why equality has been a tricky business in these theories,
103 and talk about the various attempts have been made to make the situation better.
104 One interesting development has recently emerged: Observational Type theory.  I
105 propose to explore the ways to turn these ideas into useful practices for
106 programming and theorem proving.
107
108 \section{Simple and not-so-simple types}
109
110 \subsection{Untyped $\lambda$-calculus}
111
112 Along with Turing's machines, the earliest attempts to formalise computation
113 lead to the $\lambda$-calculus \citep{Church1936}.  This early programming
114 language encodes computation with a minimal sintax and most notably no ``data''
115 in the traditional sense, but just functions.
116
117 The syntax of $\lambda$-terms consists of just three things: variables,
118 abstractions, and applications:
119
120 \newcommand{\appspace}{\hspace{0.07cm}}
121 \newcommand{\app}[2]{#1\appspace#2}
122 \newcommand{\absspace}{\hspace{0.03cm}}
123 \newcommand{\abs}[2]{\lambda #1\absspace.\absspace#2}
124 \newcommand{\termt}{t}
125 \newcommand{\termm}{m}
126 \newcommand{\termn}{n}
127 \newcommand{\termp}{p}
128 \newcommand{\termf}{f}
129 \newcommand{\separ}{\ \ |\ \ }
130 \newcommand{\termsyn}{\mathit{term}}
131 \newcommand{\axname}[1]{\textbf{#1}}
132 \newcommand{\axdesc}[2]{\axname{#1} \fbox{$#2$}}
133
134 \begin{center}
135 \axname{syntax}
136 \begin{eqnarray*}
137   \termsyn & ::= & x \separ (\abs{x}{\termsyn}) \separ (\app{\termsyn}{\termsyn}) \\
138          x & \in & \text{Some enumerable set of symbols, e.g.}\ \{x, y, z, \dots , x_1, x_2, \dots\}
139 \end{eqnarray*}
140 \end{center}
141
142
143 % I will omit parethesis in the usual manner. %TODO explain how
144
145 I will use $\termt,\termm,\termn,\dots$ to indicate a generic term, and $x,y$
146 for variables.  I will also assume that all variable names in a term are unique
147 to avoid problems with name capturing.  Intuitively, abstractions
148 ($\abs{x}{\termt}$) introduce functions with a named parameter ($x$), and
149 applications ($\app{\termt}{\termm}$) apply a function ($\termt$) to an argument
150 ($\termm$).
151
152 The ``applying'' is more formally explained with a reduction rule:
153
154 \newcommand{\bred}{\leadsto}
155 \newcommand{\bredc}{\bred^*}
156
157 \begin{center}
158 \axdesc{reduction}{\termsyn \bred \termsyn}
159 $$\app{(\abs{x}{\termt})}{\termm} \bred \termt[\termm / x]$$
160 \end{center}
161
162 Where $\termt[\termm / x]$ expresses the operation that substitutes all
163 occurrences of $x$ with $\termm$ in $\termt$.  In the systems presented, the
164 $\bred$ relation also includes reduction of subterms, for example if $\termt
165 \bred \termm$ then $\app{\termt}{\termn} \bred \app{\termm}{\termn}$, and so on.
166
167 % % TODO put the trans closure
168
169 These few elements are of remarkable expressiveness, and in fact Turing
170 complete.  As a corollary, we must be able to devise a term that reduces forever
171 (``loops'' in imperative terms):
172 \begin{equation*}
173   \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \app{(\abs{x}{\app{x}{x}})}{(\abs{x}{\app{x}{x}})} \bred \dots
174 \end{equation*}
175 Terms that can be reduced only a finite number of times (the non-looping ones)
176 are said to be \emph{normalising}, and the ``final'' term is called \emph{normal
177   form}.  These concepts (reduction and normal forms) will run through all the
178 material analysed.
179
180 \subsection{The simply typed $\lambda$-calculus}
181
182 \newcommand{\tya}{A}
183 \newcommand{\tyb}{B}
184 \newcommand{\tyc}{C}
185
186 One way to ``discipline'' $\lambda$-terms is to assign \emph{types} to them, and
187 then check that the terms that we are forming make sense given our typing rules
188 \citep{Curry1934}.
189
190 We wish to introduce rules of the form $\Gamma \vdash \termt : \tya$, which
191 reads ``in context $\Gamma$, term $\termt$ has type $\tya$''.
192
193 The syntax for types is as follows:
194
195 \newcommand{\tyarr}{\to}
196 \newcommand{\tysyn}{\mathit{type}}
197 \newcommand{\ctxsyn}{\mathit{context}}
198 \newcommand{\emptyctx}{\cdot}
199
200 \begin{center}
201   \axname{syntax}
202    $$\tysyn ::= x \separ \tysyn \tyarr \tysyn$$
203 \end{center}
204
205 I will use $\tya,\tyb,\dots$ to indicate a generic type.
206
207 A context $\Gamma$ is a map from variables to types.  We use the notation
208 $\Gamma; x : \tya$ to augment it, and to ``extract'' pairs from it.
209
210 Predictably, $\tya \tyarr \tyb$ is the type of a function from $\tya$ to
211 $\tyb$.  We need to be able to decorate our abstractions with
212 types\footnote{Actually, we don't need to: computers can infer the right type
213   easily, but that is another story.}:
214 \begin{center}
215   \axname{syntax}
216    $$\termsyn ::= x \separ (\abs{x : \tysyn}{\termsyn}) \separ (\app{\termsyn}{\termsyn})$$
217 \end{center}
218 Now we are ready to give the typing judgements:
219
220 \begin{center}
221   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
222
223   \vspace{0.5cm}
224
225   \begin{tabular}{c c c}
226     \AxiomC{}
227     \UnaryInfC{$\Gamma; x : \tya \vdash x : \tya$}
228     \DisplayProof
229     &
230     \AxiomC{$\Gamma; x : \tya \vdash \termt : \tyb$}
231     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \tya \tyarr \tyb$}
232     \DisplayProof
233   \end{tabular}
234
235   \vspace{0.5cm}
236
237   \begin{tabular}{c}
238     \AxiomC{$\Gamma \vdash \termt : \tya \tyarr \tyb$}
239     \AxiomC{$\Gamma \vdash \termm : \tya$}
240     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb$}
241     \DisplayProof
242   \end{tabular}
243 \end{center}
244
245 This typing system takes the name of ``simply typed lambda calculus'' (STLC),
246 and enjoys a number of properties.  Two of them are expected in most type
247 systems: %TODO add credit to pierce
248 \begin{description}
249 \item[Progress] A well-typed term is not stuck - either it is a value or it can
250   take a step according to the evaluation rules.  With ``value'' we mean a term
251   whose subterms (including itself) don't appear to the left of the $\bred$
252   relation.
253 \item[Preservation] If a well-typed term takes a step of evaluation, then the
254   resulting term is also well typed.
255 \end{description}
256
257 However, STLC buys us much more: every well-typed term
258 is normalising.  It is easy to see that we can't fill the blanks if we want to
259 give types to the non-normalising term shown before:
260 \begin{equation*}
261   \app{(\abs{x : ?}{\app{x}{x}})}{(\abs{x : ?}{\app{x}{x}})}
262 \end{equation*}
263
264 \newcommand{\lcfix}[2]{\mathsf{fix} \appspace #1\absspace.\absspace #2}
265
266 This makes the STLC Turing incomplete.  We can recover the ability to loop by
267 adding a combinator that recurses:
268 \begin{equation*}
269   \termsyn ::= \dots \separ  \lcfix{x : \tysyn}{\termsyn}
270 \end{equation*}
271 \begin{center}
272   \begin{prooftree}
273     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
274     \UnaryInfC{$\Gamma \vdash \lcfix{x : \tya}{\termt} : \tya$}
275   \end{prooftree}
276 \end{center}
277 \begin{equation*}
278   \lcfix{x : \tya}{\termt} \bred \termt[(\lcfix{x : \tya}{\termt}) / x]
279 \end{equation*}
280
281 However, we will keep STLC without such a facility. In the next section we shall
282 see why that is preferable for our needs.
283
284 \subsection{The Curry-Howard correspondence}
285 \label{sec:curry-howard}
286
287 \newcommand{\lcunit}{\mathsf{()}}
288
289 It turns out that the STLC can be seen a natural deduction system for
290 propositional logic.  Terms are proofs, and their types are the propositions
291 they prove.  This remarkable fact is known as the Curry-Howard correspondence,
292 or isomorphism.
293
294 The ``arrow'' ($\to$) type corresponds to implication.  If we wished to
295 prove that $(\tya \tyarr \tyb) \tyarr (\tyb \tyarr \tyc) \tyarr (\tya
296 \tyarr \tyc)$, all we need to do is to devise a $\lambda$-term that has the
297 correct type:
298 \begin{equation*}
299   \abs{f : (\tya \tyarr \tyb)}{\abs{g : (\tyb \tyarr \tyc)}{\abs{x : \tya}{\app{g}{(\app{f}{x})}}}}
300 \end{equation*}
301 That is, function composition.  We might want extend our bare lambda calculus
302 with a couple of terms to make our natural deduction more pleasant to use.  For
303 example, tagged unions (\texttt{Either} in Haskell) are disjunctions, and tuples
304 (or products) are conjunctions.  We also want to be able to express falsity, and
305 that is done by introducing a type inhabited by no terms.  If evidence of such a
306 type is presented, then we can derive any type, which expresses absurdity.
307 Conversely, $\top$ is the type with just one trivial element, $\lcunit$.
308
309 \newcommand{\lcinl}{\mathsf{inl}\appspace}
310 \newcommand{\lcinr}{\mathsf{inr}\appspace}
311 \newcommand{\lccase}[3]{\mathsf{case}\appspace#1\appspace#2\appspace#3}
312 \newcommand{\lcfst}{\mathsf{fst}\appspace}
313 \newcommand{\lcsnd}{\mathsf{snd}\appspace}
314 \newcommand{\orint}{\vee I_{1,2}}
315 \newcommand{\orintl}{\vee I_{1}}
316 \newcommand{\orintr}{\vee I_{2}}
317 \newcommand{\orel}{\vee E}
318 \newcommand{\andint}{\wedge I}
319 \newcommand{\andel}{\wedge E_{1,2}}
320 \newcommand{\botel}{\bot E}
321 \newcommand{\lcabsurd}{\mathsf{absurd}\appspace}
322
323 \begin{center}
324   \axname{syntax}
325   \begin{eqnarray*}
326     \termsyn & ::= & \dots \\
327              &  |  & \lcinl \termsyn \separ \lcinr \termsyn \separ \lccase{\termsyn}{\termsyn}{\termsyn} \\
328              &  |  & (\termsyn , \termsyn) \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
329              &  |  & \lcunit \\
330     \tysyn & ::= & \dots \separ \tysyn \vee \tysyn \separ \tysyn \wedge \tysyn \separ \bot \separ \top
331   \end{eqnarray*}
332 \end{center}
333 \begin{center}
334   \axdesc{typing}{\Gamma \vdash \termsyn : \tysyn}
335   \begin{prooftree}
336     \AxiomC{$\Gamma \vdash \termt : \tya$}
337     \RightLabel{$\orint$}
338     \UnaryInfC{$\Gamma \vdash \lcinl \termt : \tya \vee \tyb$}
339     \noLine
340     \UnaryInfC{$\Gamma \vdash \lcinr \termt : \tyb \vee \tya$}
341   \end{prooftree}
342   \begin{prooftree}
343     \AxiomC{$\Gamma \vdash \termt : \tya \vee \tyb$}
344     \AxiomC{$\Gamma \vdash \termm : \tya \tyarr \tyc$}
345     \AxiomC{$\Gamma \vdash \termn : \tyb \tyarr \tyc$}
346     \RightLabel{$\orel$}
347     \TrinaryInfC{$\Gamma \vdash \lccase{\termt}{\termm}{\termn} : \tyc$}
348   \end{prooftree}
349
350   \begin{tabular}{c c}
351     \AxiomC{$\Gamma \vdash \termt : \tya$}
352     \AxiomC{$\Gamma \vdash \termm : \tyb$}
353     \RightLabel{$\andint$}
354     \BinaryInfC{$\Gamma \vdash (\tya , \tyb) : \tya \wedge \tyb$}
355     \DisplayProof
356     &
357     \AxiomC{$\Gamma \vdash \termt : \tya \wedge \tyb$}
358     \RightLabel{$\andel$}
359     \UnaryInfC{$\Gamma \vdash \lcfst \termt : \tya$}
360     \noLine
361     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb$}
362     \DisplayProof
363   \end{tabular}
364
365   \vspace{0.5cm}
366
367   \begin{tabular}{c c}
368     \AxiomC{$\Gamma \vdash \termt : \bot$}
369     \RightLabel{$\botel$}
370     \UnaryInfC{$\Gamma \vdash \lcabsurd \termt : \tya$}
371     \DisplayProof
372     &
373     \AxiomC{}
374     \RightLabel{$\top I$}
375     \UnaryInfC{$\Gamma \vdash \lcunit : \top$}
376     \DisplayProof
377   \end{tabular}
378 \end{center}
379 \begin{center}
380   \axdesc{reduction}{\termsyn \bred \termsyn}
381   \begin{eqnarray*}
382     \lccase{(\lcinl \termt)}{\termm}{\termn} & \bred & \app{\termm}{\termt} \\
383     \lccase{(\lcinr \termt)}{\termm}{\termn} & \bred & \app{\termn}{\termt} \\
384     \lcfst (\termt , \termm)                 & \bred & \termt \\
385     \lcsnd (\termt , \termm)                 & \bred & \termm
386   \end{eqnarray*}
387 \end{center}
388
389 With these rules, our STLC now looks remarkably similar in power and use to the
390 natural deduction we already know.  $\neg A$ can be expressed as $A \tyarr
391 \bot$.  However, there is an important omission: there is no term of the type $A
392 \vee \neg A$ (excluded middle), or equivalently $\neg \neg A \tyarr A$ (double
393 negation), or indeed any term with a type equivalent to those.
394
395 This has a considerable effect on our logic and it's no coincidence, since there
396 is no obvious computational behaviour for laws like the excluded middle.
397 Theories of this kind are called \emph{intuitionistic}, or \emph{constructive},
398 and all the systems analysed will have this characteristic since they build on
399 the foundation of the STLC\footnote{There is research to give computational
400   behaviour to classical logic, but we will not touch those subjects.}.
401
402 Finally, going back to our $\mathsf{fix}$ combinator, it's now easy to see how
403 we would want to exclude such a thing if we want to use STLC as a logic, since
404 it allows us to prove everything: $(\lcfix{x : \tya}{x}) : \tya$ clearly works
405 for any $A$!  This is a crucial point: in general we wish to have systems that
406 do not let the user devise a term of type $\bot$, otherwise our logic will be
407 unsound\footnote{Obviously such a term can be present under a $\lambda$.}.
408
409 \subsection{Extending the STLC}
410
411 \newcommand{\lctype}{\mathsf{Type}}
412 \newcommand{\lcite}[3]{\mathsf{if}\appspace#1\appspace\mathsf{then}\appspace#2\appspace\mathsf{else}\appspace#3}
413 \newcommand{\lcbool}{\mathsf{Bool}}
414 \newcommand{\lcforallz}[2]{\forall #1 \absspace.\absspace #2}
415 \newcommand{\lcforall}[3]{\forall #1 : #2 \absspace.\absspace #3}
416 \newcommand{\lcexists}[3]{\exists #1 : #2 \absspace.\absspace #3}
417
418 The STLC can be made more expressive in various ways.  Henk Barendregt
419 succinctly expressed geometrically how we can expand our type system:
420
421 \begin{equation*}
422 \xymatrix@!0@=1.5cm{
423   & \lambda\omega \ar@{-}[rr]\ar@{-}'[d][dd]
424   & & \lambda C \ar@{-}[dd]
425   \\
426   \lambda2 \ar@{-}[ur]\ar@{-}[rr]\ar@{-}[dd]
427   & & \lambda P2 \ar@{-}[ur]\ar@{-}[dd]
428   \\
429   & \lambda\underline\omega \ar@{-}'[r][rr]
430   & & \lambda P\underline\omega
431   \\
432   \lambda{\to} \ar@{-}[rr]\ar@{-}[ur]
433   & & \lambda P \ar@{-}[ur]
434 }
435 \end{equation*}
436 Here $\lambda{\to}$, in the bottom left, is the STLC.  From there can move along
437 3 dimensions:
438 \begin{description}
439 \item[Terms depending on types (towards $\lambda{2}$)] In other words, we can
440   quantify over types in our type signatures: $(\abs{A : \lctype}{\abs{x : A}{x}}) : \lcforallz{A}{A \tyarr A}$.  The first and most famous instance of this idea
441   has been System F.  This gives us a form of polymorphism and has been wildly
442   successful, also thanks to a well known inference algorithm for a restricted
443   version of System F known as Hindley-Milner.  Languages like Haskell and SML
444   are based on this discipline.
445 \item[Types depending on types (towards $\lambda{\underline{\omega}}$)] In other
446   words, we have type operators: $(\abs{A : \lctype}{\abs{R : \lctype}{(A \to R) \to R}}) : \lctype \to \lctype \to \lctype$.
447 \item[Types depending on terms (towards $\lambda{P}$)] Also known as ``dependent
448   types'', give great expressive power: $(\abs{x : \lcbool}{\lcite{x}{\mathbb{N}}{\mathbb{Q}}}) : \lcbool \to \lctype$.
449 \end{description}
450
451 All the systems preserve the properties that make the STLC well behaved (some of
452 which I haven't mentioned yet).  The system we are going to focus on,
453 Intuitionistic Type Theory, has all of the above additions, and thus would sit
454 where $\lambda{C}$ sits in the ``$\lambda$-cube'' above.
455
456 \section{Intuitionistic Type Theory}
457
458 \newcommand{\lcset}[1]{\mathsf{Type}_{#1}}
459 \newcommand{\lcsetz}{\mathsf{Type}}
460 \newcommand{\defeq}{\equiv}
461
462 \subsection{A Bit of History}
463
464 Logic frameworks and programming languages based on type theory have a long
465 history.  Per Martin-L\"{o}f described the first version of his theory in 1971,
466 but then revised it since the original version was too impredicative and thus
467 inconsistent\footnote{In the early version $\lcsetz : \lcsetz$, see section
468   \ref{sec:core-tt} for an explanation on why this causes problems.}.  For this
469 reason he gave a revised and consistent definition later \citep{Martin-Lof1984}.
470
471 A related development is the one of the polymorphic $\lambda$-calculus, and
472 specifically the previously mentioned System F, which was invented independently
473 by Girard and Reynolds.  An can be found in \citep{Reynolds1994}.  The
474 surprising fact is that while System F is impredicative it is still consistent
475 and strongly normalising.  Coquand and Huet further extended this line of work
476 with the Calculus of Constructions (CoC) \citep{Coquand1986}.
477
478 \subsection{A Core Type Theory}
479 \label{sec:core-tt}
480
481 The calculus I present follows the exposition in \citep{Thompson1991}, and as
482 said previously is quite close to the original formulation of predicative ITT as
483 found in \citep{Martin-Lof1984}.
484
485 \begin{center}
486   \axname{syntax}
487   \begin{eqnarray*}
488   \termsyn & ::= & x \\
489          &  |  & \lcforall{x}{\termsyn}{\termsyn} \separ \abs{x : \termsyn}{\termsyn} \separ \app{\termsyn}{\termsyn} \\
490          &  |  & \lcexists{x}{\termsyn}{\termsyn} \separ (\termsyn , \termsyn) \separ \lcfst \termsyn \separ \lcsnd \termsyn \\
491          &  |  & \bot \separ \lcabsurd \termt \\
492          &  |  & \lcset{n} \\
493    n     & \in & \mathbb{N}
494  \end{eqnarray*}
495
496   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
497
498   \vspace{0.5cm}
499
500   \begin{tabular}{c c c}
501     \AxiomC{}
502     \RightLabel{var}
503     \UnaryInfC{$\Gamma;x : \tya \vdash x : \tya$}
504     \DisplayProof
505     &
506     \AxiomC{$\Gamma \vdash \termt : \bot$}
507     \RightLabel{$\bot E$}
508     \UnaryInfC{$\Gamma \vdash \lcabsurd \termt : A$}
509     \DisplayProof
510     &
511     \AxiomC{$\Gamma \vdash \termt : \tya$}
512     \AxiomC{$\tya \defeq \tyb$}
513     \RightLabel{$\defeq$ type}
514     \BinaryInfC{$\Gamma \vdash \termt : \tyb$}
515     \DisplayProof
516   \end{tabular}
517
518   \vspace{0.5cm}
519
520   \begin{tabular}{c c}
521     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
522     \RightLabel{$\forall I$}
523     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \lcforall{x}{\tya}{\tyb}$}
524     \DisplayProof
525     &
526     \AxiomC{$\Gamma \vdash \termt : \lcforall{x}{\tya}{\tyb}$}
527     \AxiomC{$\Gamma \vdash \termm : \tya$}
528     \RightLabel{$\forall E$}
529     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb[\termm / x]$}
530     \DisplayProof
531   \end{tabular}
532
533   \vspace{0.5cm}
534
535   \begin{tabular}{c c}
536     \AxiomC{$\Gamma \vdash \termt : \tya$}
537     \AxiomC{$\Gamma \vdash \termm : \tyb[\termt / x]$}
538     \RightLabel{$\exists I$}
539     \BinaryInfC{$\Gamma \vdash (\termt, \termm) : \lcexists{x}{\tya}{\tyb}$}
540     \DisplayProof
541     &
542     \AxiomC{$\Gamma \vdash \termt: \lcexists{x}{\tya}{\tyb}$}
543     \RightLabel{$\exists E_{1,2}$}
544     \UnaryInfC{$\hspace{0.7cm} \Gamma \vdash \lcfst \termt : \tya \hspace{0.7cm}$}
545     \noLine
546     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb[\lcfst \termt / x]$}
547     \DisplayProof
548   \end{tabular}
549
550   \vspace{0.5cm}
551
552   \begin{tabular}{c c}
553     \AxiomC{}
554     \RightLabel{type}
555     \UnaryInfC{$\Gamma \vdash \lcset{n} : \lcset{n + 1}$}
556     \DisplayProof
557     &
558     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
559     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
560     \RightLabel{$\forall, \exists$ type}
561     \BinaryInfC{$\Gamma \vdash \lcforall{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
562     \noLine
563     \UnaryInfC{$\Gamma \vdash \lcexists{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
564     \DisplayProof
565   \end{tabular}
566
567   \vspace{0.5cm}
568
569   \axdesc{reduction}{\termsyn \bred \termsyn}
570   \begin{eqnarray*}
571     \app{(\abs{x}{\termt})}{\termm} & \bred & \termt[\termm / x] \\
572     \lcfst (\termt, \termm) & \bred & \termt \\
573     \lcsnd (\termt, \termm) & \bred & \termm
574   \end{eqnarray*}
575 \end{center}
576
577 I will abbreviate $\lcset{0}$ as $\lcsetz$.
578
579 There are a lot of new factors at play here. The first thing to notice is that
580 the separation between types and terms is gone.  All we have is terms, that
581 include both values (terms of type $\lcset{0}$) and types (terms of type
582 $\lcset{n}$, with $n > 0$).  This change is reflected in the typing rules.
583 While in the STLC values and types are kept well separated (values never go
584 ``right of the colon''), in ITT types can freely depend on values.
585
586 This relation is expressed in the typing rules for $\forall$ and $\exists$: if a
587 function has type $\lcforall{x}{\tya}{\tyb}$, $\tyb$ can depend on $x$.
588 Examples will make this clearer once some base types are added in the next
589 section.
590
591 $\forall$ and $\exists$ are at the core of the machinery of ITT:
592
593 \begin{description}
594 \item[``forall'' ($\forall$)] is a generalisation of $\tyarr$ in the STLC and
595   expresses universal quantification in our logic.  In the literature this is
596   also known as ``dependent product'' and shown as $\Pi$, following the
597   interpretation of functions as infinitary products. We will just call it
598   ``dependent function'', reserving ``product'' for $\exists$.
599
600 \item[``exists'' ($\exists$)] is a generalisation of $\wedge$ in the extended
601   STLC of section \ref{sec:curry-howard}, and thus we will call it ``dependent
602   product''.  Like $\wedge$, it is formed by providing a pair of things.  In our
603   logic, it represents existential quantification.
604
605   For added confusion, in the literature that calls $\forall$ $\Pi$, $\exists$
606   is often named ``dependent sum'' and shown as $\Sigma$.  This is following the
607   interpretation of $\exists$ as a generalised, infinitary $\vee$, where the
608   first element of the pair is the ``tag'' that decides which type the second
609   element will have.
610 \end{description}
611
612 Another thing to notice is that types are very ``first class'': we are free to
613 create functions that accept and return types.  For this reason we $\defeq$ as
614 the smallest equivalence relation extending $\bredc$, where $\bredc$ is the
615 reflexive transitive closure of $\bred$; and we treat types that are equal
616 according to $\defeq$ as the same.  Another way of seeing $\defeq$ is this: when
617 we want to compare two types for equality, we reduce them as far as possible and
618 then check if they are equal\footnote{Note that when comparing terms we do it up
619   to $\alpha$-renaming.  That is, we do not consider relabelling of variables as
620   a difference - for example $\abs{x : A}{x} \defeq \abs{y : A}{y}$.}.  This
621 works since not only each term has a normal form (ITT is strongly normalising),
622 but the normal form is also unique; or in other words $\bred$ is confluent (if
623 $\termt \bredc \termm$ and $\termt \bredc \termn$, then $\termm \bredc \termp$
624 and $\termn \bredc \termp$).  This measure makes sure that, for instance,
625 $\app{(\abs{x : \lctype}{x})}{\lcbool} \defeq \lcbool$.  The theme of equality
626 is central and will be analysed better later.
627
628 The theory presented is \emph{stratified}.  We have a hierarchy of types
629 $\lcset{0} : \lcset{1} : \lcset{2} : \dots$, so that there is no ``type of all
630 types'', and our theory is predicative.  The layers of the hierarchy are called
631 ``universes''.  $\lcsetz : \lcsetz$ ITT is inconsistent due to Girard's paradox
632 \citep{Hurkens1995}, and thus loses its well-behavedness.  Some impredicativity
633 sometimes has its place, either because the theory retain good properties
634 (normalization, consistency, etc.) anyway, like in System F and CoC; or because
635 we are at a stage at which we do not care - we will see instances of the last
636 motivation later.  Moreover, universes can be inferred mechanically
637 \citep{Pollack1990}. It is also convenient to have a \emph{cumulative} theory,
638 where $\lcset{n} : \lcset{m}$ iff $n < m$.  We eschew these measures to keep the
639 presentation simple.
640
641 Lastly, the theory I present is fully explicit in the sense that the user has to
642 specify every type when forming abstractions, products, etc.  This can be a
643 great burden if one wants to use the theory directly.  Complete inference is
644 undecidable (which is hardly surprising considering the role that types play)
645 but partial inference (also called ``bidirectional type checking'' in this
646 context) in the style of \citep{Pierce2000} will have to be deployed in a
647 practical system.  When showing examples obvious types will be omitted.
648
649 Note that the Curry-Howard correspondence runs through ITT as it did with the
650 STLC with the difference that ITT corresponds to an higher order propositional
651 logic.
652
653 \subsection{Base Types}
654
655 While the ITT presented is a fairly complete logic, it is not that useful for
656 programming.  If we wish to make it better, we can add some base types to
657 represent the data structures we know and love, such as numbers, lists, and
658 trees.
659
660 \newcommand{\lctrue}{\mathsf{true}}
661 \newcommand{\lcfalse}{\mathsf{false}}
662
663 \begin{center}
664   \axname{syntax}
665   \begin{eqnarray*}
666   \termsyn & ::= & ... \\
667            &  |  & \lcbool \separ \lctrue \separ \lcfalse
668  \end{eqnarray*}
669
670   \axdesc{typing}{\Gamma \vdash \termsyn : \termsyn}
671
672   \vspace{0.5cm}
673
674   \begin{tabular}{c c c}
675     \AxiomC{}
676     \RightLabel{var}
677     \UnaryInfC{$\Gamma;x : \tya \vdash x : \tya$}
678     \DisplayProof
679     &
680     \AxiomC{$\Gamma \vdash \termt : \bot$}
681     \RightLabel{$\bot E$}
682     \UnaryInfC{$\Gamma \vdash \lcabsurd \termt : A$}
683     \DisplayProof
684     &
685     \AxiomC{$\Gamma \vdash \termt : \tya$}
686     \AxiomC{$\tya \defeq \tyb$}
687     \RightLabel{$\defeq$ type}
688     \BinaryInfC{$\Gamma \vdash \termt : \tyb$}
689     \DisplayProof
690   \end{tabular}
691
692   \vspace{0.5cm}
693
694   \begin{tabular}{c c}
695     \AxiomC{$\Gamma;x : \tya \vdash \termt : \tya$}
696     \RightLabel{$\forall I$}
697     \UnaryInfC{$\Gamma \vdash \abs{x : \tya}{\termt} : \lcforall{x}{\tya}{\tyb}$}
698     \DisplayProof
699     &
700     \AxiomC{$\Gamma \vdash \termt : \lcforall{x}{\tya}{\tyb}$}
701     \AxiomC{$\Gamma \vdash \termm : \tya$}
702     \RightLabel{$\forall E$}
703     \BinaryInfC{$\Gamma \vdash \app{\termt}{\termm} : \tyb[\termm / x]$}
704     \DisplayProof
705   \end{tabular}
706
707   \vspace{0.5cm}
708
709   \begin{tabular}{c c}
710     \AxiomC{$\Gamma \vdash \termt : \tya$}
711     \AxiomC{$\Gamma \vdash \termm : \tyb[\termt / x]$}
712     \RightLabel{$\exists I$}
713     \BinaryInfC{$\Gamma \vdash (\termt, \termm) : \lcexists{x}{\tya}{\tyb}$}
714     \DisplayProof
715     &
716     \AxiomC{$\Gamma \vdash \termt: \lcexists{x}{\tya}{\tyb}$}
717     \RightLabel{$\exists E_{1,2}$}
718     \UnaryInfC{$\hspace{0.7cm} \Gamma \vdash \lcfst \termt : \tya \hspace{0.7cm}$}
719     \noLine
720     \UnaryInfC{$\Gamma \vdash \lcsnd \termt : \tyb[\lcfst \termt / x]$}
721     \DisplayProof
722   \end{tabular}
723
724   \vspace{0.5cm}
725
726   \begin{tabular}{c c}
727     \AxiomC{}
728     \RightLabel{type}
729     \UnaryInfC{$\Gamma \vdash \lcset{n} : \lcset{n + 1}$}
730     \DisplayProof
731     &
732     \AxiomC{$\Gamma \vdash \tya : \lcset{n}$}
733     \AxiomC{$\Gamma; x : \tya \vdash \tyb : \lcset{m}$}
734     \RightLabel{$\forall, \exists$ type}
735     \BinaryInfC{$\Gamma \vdash \lcforall{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
736     \noLine
737     \UnaryInfC{$\Gamma \vdash \lcexists{x}{\tya}{\tyb} : \lcset{n \sqcup m}$}
738     \DisplayProof
739   \end{tabular}
740
741   \vspace{0.5cm}
742
743   \axdesc{reduction}{\termsyn \bred \termsyn}
744   \begin{eqnarray*}
745     \app{(\abs{x}{\termt})}{\termm} & \bred & \termt[\termm / x] \\
746     \lcfst (\termt, \termm) & \bred & \termt \\
747     \lcsnd (\termt, \termm) & \bred & \termm
748   \end{eqnarray*}
749 \end{center}
750
751
752 \bibliographystyle{authordate1}
753 \bibliography{background}
754
755 \end{document}