on 73ec76f help sign in
Idris-dev/papers/impl-paper/typechecking.tex
605 lines 22 kB history raw
1\section{The Core Type Theory}
3\label{sect:typechecking}
5High level \Idris{} programs, as described in Section \ref{sect:hll}, are
6\remph{elaborated} to a small core language, \TT{}, then type checked.
7\TT{} is a dependently typed $\lambda$-calculus with inductive families
8and pattern matching definitions similar to UTT~\cite{luo1994}, and building
9on an earlier implementation, \Ivor{}~\cite{Brady2006b}.
10The
11core language is kept as small as is reasonably possible, which has several advantages: it is
12easy to type check, since type checking dependent type theory is well understood
13~\cite{loh2010tutorial}; and it is easy to transform, optimise and compile. Keeping
14the core language small increases confidence that these important components of
15the language are correct. In this section, we describe \TT{} and
16its semantics.
18\subsection{\TT{} syntax}
20The syntax of \TT{} expressions is given in Figure \ref{ttsyn}. This defines:
22\begin{itemize}
23\item \demph{Terms}, $\vt$, which are variables, bindings, applications or constants.
24\item \demph{Bindings}, $\vb$, which are lambda abstractions, let bindings, or function spaces.
25\item \demph{Constants}, $\vc$, which are integer or string literals, or $\Type_i$, the
26type of types, and may be extended with other primitives.
27\end{itemize}
29The function space $\all{\vx}{\vS}\SC\vT$ may also be written as $(\vx\Hab\vS)\to\vT$,
30or $\vS\to\vT$ if $\vx$ is not free in $\vT$, to make the notation more consistent with
31the high level language. Universe levels on types ($\Type_i$) may be left implicit and
32inferred by the machine~\cite{pollack1990implicit}.
34\FFIG{
35\begin{array}{rll@{\hg}rll}
36\vt ::= & \vc & (\mbox{constant}) &
37\hg\vb ::= & \lam{\vx}{\vt} & (\mbox{abstraction}) \\
39 \mid & \vx & (\mbox{variable}) &
40 \mid & \LET\:\vx\defq\vt\Hab\vt & (\mbox{let binding}) \\
42 \mid & \vb\SC\:\vt & (\mbox{binding}) &
43 \mid & \all{\vx}{\vt} & (\mbox{function space}) \\
45 \mid & \vt\;\vt & (\mbox{application}) &
46% \mid & \pat{\vx}{\vt} & (\mbox{pattern variable}) \\
47% & & &
48% \mid & \pty{\vx}{\vt} & (\mbox{pattern type}) \\
49\medskip\\
50 \vc ::= & \Type_i & (\mbox{type universes}) \\
51 \mid & \vi & (\mbox{integer literal}) \\
52 \mid & \VV{str} & (\mbox{string literal}) \\
53\end{array}
55{\TT{} expression syntax}
56{ttsyn}
58A \TT{} program is a collection of \demph{inductive family} definitions (Section
59\ref{sect:inductivefams}) and \demph{pattern matching} function definitions (Section
60\ref{sect:patdefs}), as well as primitive operators on constants.
61Before defining these, let us define the semantics of \TT{}
62expressions.
64\subsection{\TT{} semantics}
66The static and dynamic semantics of \TT{} are defined mutually, since
67evaluation relies on a well-typed term, and type checking relies on
68evaluation. Everything is defined relative
69to a context, $\Gamma$. The empty context
70is valid, as is a context extended with a $\lambda$, $\forall$ or
71$\LET$ binding:
73\DM{
74\Axiom{\proves\RW{valid}}
75\hg
76\Rule{\Gamma\proves\vS\Hab\Type_i}
77{\Gamma;\lam{\vx}{\vS}\proves\RW{valid}}
78\hg
79\Rule{\Gamma\proves\vS\Hab\Type_i}
80{\Gamma;\all{\vx}{\vS}\proves\RW{valid}}
81\hg
82\Rule{\Gamma\proves\vS\Hab\Type_i\hg\Gamma\proves\vs\Hab\vS}
83{\Gamma;\LET\:\vx\:\defq\:\vs\Hab\vS\proves\RW{valid}}
86\subsubsection{Evaluation}
88\label{sect:evaluation}
90Evaluation of \TT{} is defined by contraction schemes, given in Figure
91\ref{ttcontract}. \demph{Contraction}, relative to a context $\Gamma$, is given
92by one of the following schemes:
94\begin{itemize}
95\item $\beta$-contraction, which substitutes a value applied to a $\lambda$-binding for
96the bound variable. We define $\beta$-contraction simply by replacing the $\lambda$-binding
97with a $\LET$ binding.
98\item $\delta$-contraction, which replaces a $\LET$ bound variable with its value.
99\end{itemize}
101\noindent
102The following contextual closure rule reduces a $\LET$ binding by creating
103a $\delta$-reducible expression:
105\DM{
106\Rule{\Gamma;\LET\:\vx\defq\vs\Hab\vS\proves\vt\leadsto\vu}
107{\Gamma\proves\LET\:\vx\defq\vs\Hab\vS\SC\vt\leadsto\vu}
110\demph{Reduction} ($\reduces$) is the structural closure of contraction, and evaluation
111is ($\reducesto$) is the transitive closure of reduction. \demph{Conversion} ($\converts$)
112is the smallest equivalence relation closed under reduction. If $\Gamma\proves\vx\converts\vy$
113then $\vy$ can be obtained from $\vx$ by a finite, possibly empty, sequence of
114contractions and reversed contractions. Terms which are convertible are also said to
115be definitionally equal.
116The evaluator can also be extended by defining pattern matching functions, which
117will be described in more detail in Section \ref{sect:patdefs}. In principle, pattern
118matching functions can be understood as extending the core language with high level
119reduction rules.
121\FFIG{
122\begin{array}{lc}
123\beta\mathrm{-contraction} &
124\Axiom{
125\Gamma\proves(\lam{\vx}{\vS}\SC\vt)\:\vs\leadsto\LET\:\vx\defq\vs\Hab\vS\SC\vt
126} \\
127\delta\mathrm{-contraction} &
128\Axiom{
129\Gamma;\LET\:\vx\defq\vs\Hab\vS;\Gamma'
130\proves
131\vx\leadsto\vs
133\end{array}
135{\TT{} contraction schemes}
136{ttcontract}
140\subsubsection{Typing rules}
142\label{sect:typerules}
144The type inference rules for \TT{} expressions are given in Figure \ref{typerules}.
145These rules use the \demph{cumulativity} relation, defined in
146Figure \ref{cumul}. The type of types, $\Type_i$ is parameterised by a universe level,
147to prevent Girard's paradox~\cite{coquand1986analysis}. Cumulativity allows programs at
148lower universe levels to inhabit higher universe levels. In practice, universe levels
149can be left implicit (and will be left implicit in the rest of this paper) ---
150the type checker generates a graph of constraints between universe levels (such
151as that produced by the $\mathsf{Forall}$ typing rule) and checks that there
152are no cycles. Otherwise, the typing rules are standard and type inference can
153be implemented in the usual way~\cite{loh2010tutorial}.
155\FFIG{\begin{array}{c}
156\mathsf{Type}\:
157\Rule{\Gamma\proves\RW{valid}}
158{\Gamma\vdash\Type_n\Hab\Type_{n+1}}
159\\
160\mathsf{Const}_1\:
161\Rule{\Gamma\proves\RW{valid}}
162{\Gamma\vdash\vi\Hab\TC{Int}}
163\hg
164\mathsf{Const}_2\:
165\Rule{\Gamma\proves\RW{valid}}
166{\Gamma\vdash\VV{str}\Hab\TC{String}}
167\\
168\mathsf{Const}_3\:
169\Rule{\Gamma\proves\RW{valid}}
170{\Gamma\vdash\TC{Int}\Hab\Type_0}
171\hg
172\mathsf{Const}_4\:
173\Rule{\Gamma\proves\RW{valid}}
174{\Gamma\vdash\TC{String}\Hab\Type_0}
175\\
176%\mathsf{Pat}_1\:
177%\Rule{(\pat{\vx}{\vS})\in\Gamma}
178%{\Gamma\vdash\vx\Hab\vS}
179%\hg
180%\mathsf{Pat}_2\:
181%\Rule{(\pty{\vx}{\vS})\in\Gamma}
182%{\Gamma\vdash\vx\Hab\vS}
183\\
184\mathsf{Var}_1\:
185\Rule{(\lam{\vx}{\vS})\in\Gamma}
186{\Gamma\vdash\vx\Hab\vS}
187\hg
188\mathsf{Var}_2\:
189\Rule{(\all{\vx}{\vS})\in\Gamma}
190{\Gamma\vdash\vx\Hab\vS}
191\hg
192\mathsf{Val}\:
193\Rule{(\LET\:\vx\Hab\vS\defq\vs)\in\Gamma}
194{\Gamma\vdash\vx\Hab\vS}
195\\
196\mathsf{App}\:
197\Rule{\Gamma\vdash\vf\Hab\fbind{\vx}{\vS}{\vT}\hg\Gamma\vdash\vs\Hab\vS}
198{\Gamma\vdash\vf\:\vs\Hab\vT[\vs/\vx]} % \LET\:\vx\Hab\vS\:\defq\:\vs\:\IN\:\vT}
199\\
200\mathsf{Lam}\:
201\Rule{\Gamma;\lam{\vx}{\vS}\vdash\ve\Hab\vT\hg\Gamma\proves\fbind{\vx}{\vS}{\vT}\Hab\Type_n}
202{\Gamma\vdash\lam{\vx}{\vS}.\ve\Hab\fbind{\vx}{\vS}{\vT}}
203\\
204\mathsf{Forall}\:
205\Rule{\Gamma;\all{\vx}{\vS}\vdash\vT\Hab\Type_m\hg\Gamma\vdash\vS\Hab\Type_n}
206{\Gamma\vdash\fbind{\vx}{\vS}{\vT}\Hab\Type_p}
207\:(\exists\vp.\vm\le\vp,\:\vn\le\vp)
208\\
209\mathsf{Let}\:
210\Rule{\begin{array}{c}\Gamma\proves\ve_1\Hab\vS\hg
211 \Gamma;\LET\:\vx\defq\ve_1\Hab\vS\proves\ve_2\Hab\vT\\
212 \Gamma\proves\vS\Hab\Type_n\hg
213 \Gamma;\LET\:\vx\defq\ve_1\Hab\vS\proves\vT\Hab\Type_n\end{array}
214 }
215{\Gamma\vdash\LET\:\vx\defq\ve_1\Hab\vS\SC\:\ve_2\Hab
216 \vT[\ve_1/\vx]}
217%\Let\:\vx\Hab\vS\defq\ve_1\:\IN\:\vT}
218\\
220\mathsf{Conv}\:
221\Rule{\Gamma\proves\vx\Hab\vA\hg\Gamma\proves\vA'\Hab\Type_n\hg
222 \Gamma\proves\vA\cumul\vA'}
223 {\Gamma\proves\vx\Hab\vA'}
224\end{array}
226{Typing rules for \TT{}}
227{typerules}
229\FFIG{
230\begin{array}{c}
231\Rule{\Gamma\proves\vS\converts\vT}
232{\Gamma\proves\vS\cumul\vT}
233\hg
234\Axiom{\Gamma\proves\Type_n\cumul\Type_{n+1}}
235\\
236\Rule{\Gamma\proves\vR\cumul\vS\hg\Gamma\proves\vS\cumul\vT}
237{\Gamma\proves\vR\cumul\vT}
238\\
239\Rule{\Gamma\proves\vS_1\converts\vS_2\hg\Gamma;\vx\Hab\vS_1\proves\vT_1\cumul\vT_2}
240{\Gamma\proves\all{\vx}{\vS_1}\SC\vT_1\cumul\all{\vx}{\vS_2}{\vT_2}}
241\end{array}
243{Cumulativity}
244{cumul}
246\subsection{Inductive Families}
248\label{sect:inductivefams}
250Inductive families \cite{dybjer1994inductive} are a form of simultaneously
251defined collection of algebraic data types which can be parameterised over
252\remph{values} as well as types. An inductive family is declared
253in a similar style to a Haskell GADT declaration~\cite{pj2006gadts}
254as
255follows, using vector notation, $\tx$, to indicate a
256sequence of zero or more $\vx$ (i.e., $\vx_1,\vx_2,\ldots,\vx_n$):
258\DM{
259\AR{
260\Data\hg\TC{T}\:(\tx\Hab\ttt)\Hab\vt\hg\Where\hg
261\DC{c}_1\Hab\vt\:\mid\:\ldots\:\mid\:\DC{c}_n\Hab\vt
265Constructors may take recursive arguments in the family $\TC{T}$, which may be
266higher order. Higher order recursive arguments may be computed from any type
267which does not involve $\TC{T}$ itself. This restriction is known as
268\demph{strict positivity} and ensures that recursive arguments of the
269constructor are structurally smaller than the value itself.
271For example, the \Idris{} data type \tTC{Nat} would be declared in \TT{} as follows:
273\DM{
274\Data\hg\Nat\Hab\Type\hg\Where\hg\Z\Hab\Nat\:\mid\:\suc\Hab\fbind{\vk}{\Nat}{\Nat}
277A data type may have zero or more parameters (which are invariant
278across a structure) and a number of indices, given by the type. For
279example, the \TT{} equivalent of \tTC{List} is parameterised over its element type:
281\DM{
282\AR{
283\Data\hg\List\:(\va\Hab\Type)\Hab\Type\hg\Where
284\ARd{
285& \nil\Hab\List\:\va\\
286\mid & (\cons)\Hab\fbind{\vx}{\va}{\fbind{\vxs}{\List\:\va}{\List\:\va}}
291Types can be
292parameterised over values. Using this, we can declare the type of
293vectors (lists with length), where the empty list is statically known
294to have length zero, and the non empty list is statically known to
295have a non zero length. The \TT{} equivalent of \tTC{Vect} is parameterised over its element type,
296like $\List$, but \remph{indexed} over its length. Note also that the length
297index $\vk$ is given \remph{explicitly}.
299\DM{
300\AR{
301\Data\hg\Vect\:(\va\Hab\Type)\Hab\Nat\to\Type\hg\Where \\
302\hg\hg\ARd{
303& \nil\Hab\Vect\:\va\:\Z\\
304\mid & (\cons)\Hab\fbind{\vk}{\Nat}{
305\fbind{\vx}{\va}{\fbind{\vxs}{\Vect\:\va\:\vk}{\Vect\:\va\:(\suc\:\vk)}}
311\subsection{Pattern matching definitions}
313\label{sect:patdefs}
315\subsubsection{Syntax}
317A pattern matching definition for a function named $\FN{f}$ takes the following form,
318consisting of a type declaration followed by one or more pattern clauses:
320\DM{
321\AR{
322\FN{f}\Hab\vt\\
323\pat{\tx_1}{\ttt_1}\SC\FN{f}\:\ttt_1\:=\:\vt_1\\
324\ldots\\
325\pat{\tx_n}{\ttt_n}\SC\FN{f}\:\ttt_n\:=\:\vt_n\\
329A pattern clause consists of a list of pattern variable bindings, introduced by
330$\RW{var}$,
331and a left and right hand side, both of which
332are \TT{} expressions. Each side is type checked relative to the variable bindings,
333and the types of each side must be convertible. Additionally, the
334left hand side must take the form of $\FN{f}$ applied to a number of \TT{} expressions,
335and the number of arguments must be the same in each clause. The right hand
336sides may include applications of $\FN{f}$, i.e. pattern matching definitions may
337be recursive. Termination analysis is implemented separately. The validity of a pattern
338clause is defined by the following rule:
340\DM{
341\Rule{
342\Gamma;\lam{\tx}{\tU}\proves\FN{f}\:\tts\Hab\vS\hg
343\Gamma;\lam{\tx}{\tU}\proves\ve\Hab\vT\hg
344\Gamma\proves\vS\converts\vT}
346\Gamma\proves\pat{\tx}{\tU}\SC\FN{f}\:\tts\:=\:\ve\:\RW{valid}
350A valid pattern matching definition effectively extends \TT{} with a new constant,
351with the given type (extending the initial typing rules given in Section \ref{sect:typerules})
352and reduction behaviour (extending the initial reduction rules given in Section
353\ref{sect:evaluation}).
355\subsubsection{Semantics}
357Matching on an expression proceeds by comparing the expression to
358each match clause in order, resulting in either:
360\begin{itemize}
361\item \demph{Success}, with pattern variables mapping to expressions
362\item \demph{Failure}, with matching continuing by proceeding to the next match clause
363\item Matching being \demph{blocked}, for example by attempting to match a variable
364against a constructor pattern. In this case, no reduction occurs, because instantiating
365the variable may provide enough information for the clause to match.
366\end{itemize}
368% Pat(x args) => x (Pat' args)
369% Pat (x) => x
370% Pat _ => _
372In order to implement pattern matching, we must separate the \remph{accessible} and
373\remph{inaccessible} patterns. A pattern is \demph{accessible} (that is, it is possible
374to match against it) if it is constructor headed, or a variable. Inaccessible patterns
375are converted to ``match anything'' patterns. Clauses are converted to matchable pattern
376clauses with the $\MO{Clause}$ operation, given in Figure \ref{mkclause}.
377We extend the vector notation to meta-operations: the notation $\vec{\MO{Pat}}$ lifts the
378$\MO{Pat}$ operation across a list of arguments.
380\FFIG{
381\AR{
382\PA{\A}{
383& \MO{Clause} & (\pat{\tx}{\tU}\SC\FN{f}\:\tts\:=\:\ve)
384 & \MoRet{\FN{f}\:(\vec{\MO{Pat}}\:\tts)\:=\:\ve}
386\medskip\\
387\PA{\A}{
388& \MO{Pat} & (\vx\:\tts) & \MoRet{\vx\:(\vec{\MO{Pat}}\:\tts)
389 \hg\mbox{(if $\MO{Con}\:\vx$ and $\MO{Arity}\:\vx = \MO{Length}\:\tts$)}} \\
390& \MO{Pat} & \vx & \MoRet{\vx} \hg\mbox(if $\vx$ is a pattern variable)\\
391& \MO{Pat} & \cdot & \MoRet{\_} \hg\mbox(in all other cases)\\
395{Building matchable pattern clauses}
396{mkclause}
399% Match (c args, c args') => Match' (args, args')
400% Match (c args, x) => Blocked
401% Match (x, t) => Success (x => t)
402% Match (_, t) => Success ()
404\newcommand{\Blocked}{\mathsf{Blocked}}
405\newcommand{\Success}{\mathsf{Success}}
406\newcommand{\Failure}{\mathsf{Failure}}
408Pattern matching is invoked by the evaluator on encountering a function with
409a pattern matching definition applied to some arguments. If successful, pattern
410matching returns a new expression, which can then be reduced further.
411Figure \ref{pmatch} gives the semantics of pattern matching a collection of
412matchable clauses
413$\tc$ against an expression $\ve$. $\MO{Match}$ attempts to match each clause in
414turn against $\ve$. If matching a clause returns $\Success$ or $\Blocked$, then
415matching terminates. If matching a clause returns $\Failure$, then matching proceeds
416with the next clause. The algorithm uses the following meta-operations:
418\begin{itemize}
419\item $\MO{Con}\:\vx$, which returns true if $\vx$ is a constructor name
420\item $\MO{Arity}\:\vx$, which, if $\vx$ is a constructor name, returns the number of arguments
421$\vx$ requires.
422\item $\MO{Length}\:\tts$, which returns the length of the vector $\tts$
423\end{itemize}
426\FFIG{
427\AR{
428\PA{\A\A}{
429& \MO{MatchArg} & (\vx\:\tts) & (\vx\:\tts')
430 & \MoRet{\vec{\MO{MatchArg}}\:\tts\:\tts'\hg\mbox{(if $\MO{Con}\:\vx$)}}\\
431& \MO{MatchArg} & (\vx\:\tts) & \vx & \MoRet{\Blocked} \\
432& \MO{MatchArg} & \vx & \vt & \MoRet{\Success\:(\vx\mapsto\vt)} \\
433& \MO{MatchArg} & \_ & \vt & \MoRet{\Success\:()} \\
434& \MO{MatchArg} & \cdot & \cdot & \MoRet{\Failure}
436\medskip\\
437\PA{\A\A}{
438& \MO{MatchClause} & (\vf\:\tp = \ve) & (\vf\:\tts) &
439 \MoRet{
440 \AR{
441 \Success\:\ve[\tx/\tv]\hg\mbox{(if $\vec{\MO{MatchArg}}\:\tp\:\tts \mq \Success\:(\tx,\tv)$)}\\
442 \Blocked\;\;\;\hg\hg\mbox{(if $\vec{\MO{MatchArg}}\:\tp\:\tts \mq \Blocked$)}\\
443 \Failure\;\;\;\;\;\hg\hg\mbox{(if $\vec{\MO{MatchArg}}\:\tp\:\tts \mq \Failure$)}\\
444 }
445 } \\
446& \MO{MatchClause} & (\vf\:\tp = \ve) & \cdot & \MoRet{\Failure} \\
448\medskip\\
449\PA{\A\A}{
450& \MO{Match} & (\vc ; \tc) & \ve &
451 \MoRet{
452 \AR{
453 \Success\;\vx\;\;\hg\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Success\:\vx)$} \\
454 \Blocked\hg\;\;\;\;\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Blocked)$} \\
455 \MO{Match}\:\tc\:\ve\hg\mbox{(otherwise)}
456 }
457 } \\
458& \MO{Match} & \cdot & \ve & \MoRet{\Failure}
462{Pattern matching semantics}
463{pmatch}
465By convention, we write the application of a meta-operation $\MO{Op}$
466across a vector $\tx$ as $\vec{\MO{Op}}\:\tx$.
467In practice, for efficiency, pattern matching is implemented by compiling the match clauses to
468a tree of case expressions~\cite{Augustsson1985}.
470\subsection{Totality checking}
472In order to ensure termination of type checking
473we must distinguish terms for which evaluation definitely terminates, and
474those which may diverge. \TT{} takes a simple but pragmatic and
475effective approach to termination
476checking: any functions which do not satisfy a syntactic constraint on recursive
477calls are marked as \emph{partial}. Additionally, any function which calls a partial
478function or uses a data type which is not strictly positive is also marked as
479partial. The syntactic constraint we use is that a recursive call in a pattern
480clause must have an argument which is structurally smaller than an input argument
481in the same position. \TT{} also marks functions which do not cover all possible
482inputs as partial. This totality checking is independent of the rest of the type
483theory, and can be extended.
485This approach, separating the termination requirement from the type theory, means that
486an \Idris{} programmer makes the decision about the importance of totality for each
487function rather than having the totality requirement imposed by the type theory.
489\subsection{From \Idris{} to \TT{}}
491\TT{} is a very small language, consisting only of data declarations and pattern matching
492function definitions. There is no significant innovation in the design of \TT{}, and this
493is a deliberate choice --- it is a combination of small, well-understood components.
494The kernel of the \TT{} implementation, consisting of a type checker, evaluator and
495pattern match compiler, is less than 1000 lines of Haskell code. If we are confident
496in the correctness of the kernel of \TT{}, and any higher level language feature
497can be translated into \TT{}, we can be more confident of the correctness of the high
498level language implementation than if it were implemented directly.
500The process of elaborating a high level language into a core type theory like \TT{} is,
501however, less well understood, and presents several challenges depending on the
502features of the high level language.
503%Elaboration has been implemented in various different ways,
504%for example in Agda~\cite{norell2007thesis} and Epigram~\cite{McBride2004a}.
506\subsubsection{Example Elaboration}
508Recall the following \Idris{} function:
510\useverb{vadd}
512\noindent
513In order to elaborate
514this to \TT{}, we must resolve the implicit arguments, and make the type class explicit.
515The first step is to make the implicit arguments explicit, using a placeholder
516to stand for the arguments we have not yet resolved. The type class argument is
517also treated as an implicit argument, to which we give the name \texttt{c}:
519\begin{SaveVerbatim}{vAddImp}
521vAdd : (a : _) -> (n : _) -> Num a -> Vect a n -> Vect a n -> Vect a n
522vAdd _ _ c (Nil _) (Nil _) = (Nil _)
523vAdd _ _ c ((::) _ _ x xs) ((::) _ _ y ys)
524 = (::) _ _ ((+) _ x y) (vAdd _ _ _ xs ys)
526\end{SaveVerbatim}
527\useverb{vAddImp}
529Next, we resolve the implicit arguments. Each implicit argument can only take
530one value for this program to be type correct --- these are solved by a unification
531algorithm:
533\begin{SaveVerbatim}{vAddImpSolve}
535vAdd : (a : Type) -> (n : Nat) -> Num a -> Vect a n -> Vect a n -> Vect a n
536vAdd a O c (Nil a) (Nil a) = Nil a
537vAdd a (S k) c ((::) a k x xs) ((::) a k y ys)
538 = (::) a k ((+) c x y) (vAdd a k c xs ys)
540\end{SaveVerbatim}
541\useverb{vAddImpSolve}
543Finally, to build the \TT{} definition, we need to find the type of each pattern variable
544and state it explicitly. This leads to the \TT{} definition in Figure \ref{ttvadd},
545switching to \TT{} notation from the ASCII \Idris{} syntax.
547\FFIG{
548\AR{
549\begin{array}{ll}
550\FN{vAdd} & \Hab(\va\Hab\Type)\to(\vn\Hab\Nat)\to\TC{Num}\:\va\to
551 \Vect\:\va\:\vn\to\Vect\:\va\:\vn\to\Vect\:\va\:\vn\\
552\end{array}
553\\
554\begin{array}{lccccccc}
555\multicolumn{5}{l}{
556\RW{var}\:\va\Hab\Type,\:\vc\Hab\TC{Num}\;\va\SC}\\
557\hg\FN{vAdd} & \va & \Z & \vc & (\DC{Nil}\;\va) & (\DC{Nil}\;\va) & \Ret{\DC{Nil}\;\va} \\
558\multicolumn{5}{l}{
559\RW{var}\:\AR{
560\va\Hab\Type,\:\vk\Hab\Nat,\;\vc\Hab\TC{Num}\;\va,\\
561\vx\Hab\va,\;\vxs\Hab\Vect\;\va\;\vk,\;
562\vy\Hab\va,\;\vys\Hab\Vect\;\va\;\vk
563\SC
565}\\
566\hg\FN{vAdd} & \va & (\suc\;\vk) & \vc
567 & ((\DC{::})\;\va\;\vk\;\vx\;\vxs)
568 & ((\DC{::})\;\va\;\vk\;\vy\;\vys)
569 & \\
570 & & \MRet{5}{((\DC{::})\;\va\;\vk\;((+)\;\vc\;\vx\;\vy)\; (\FN{vAdd}\;\va\;\vk\;\vc\;\vxs\;\vys))}
571\end{array}
574{Pairwise addition of vectors in \TT{}}
575{ttvadd}
577\subsubsection{An observation}
579\Idris{} programs may contain several high level constructs not present in \TT{}, such
580as implicit arguments, type classes, \texttt{where} clauses, pattern matching \texttt{let}
581and \texttt{case} constructs. We would like the high level language to be as expressive
582as possible, while remaining possible to translate to \TT{}.
584Before considering how to achieve this, we make an observation about the distinction between
585programming and theorem proving with dependent types, and appropriate mechanisms for
586constructing programs and theorems:
588\begin{itemize}
589\item \remph{Pattern matching} is a convenient abstraction for humans to write
590programs, in that it allows a programmer to express exactly the computational
591behaviour of a function.
592\item \remph{Tactics}, such as those used in the Coq theorem
593prover~\cite{Bertot2004}, are a convenient abstraction for building proofs and
594programs by \remph{refinement}.
595\end{itemize}
597The idea behind the \Idris{} elaborator, therefore, is to use the high level
598program to direct \demph{tactics} to build \TT{} programs by refinement. The
599elaborator is implemented as a Haskell monad capturing proof state, with a
600collection of tactics for updating and refining the proof state. The remainder
601of this paper describes this elaborator and demonstrates how it is used to
602implement the high level features of \Idris{}.