on master help sign in
Lots of implementation paper revision
0d11515Edwin Brady
papers/impl-paper/elaboration.tex+407−195
@@ -1,4 +1,5 @@
1\newcommand{\ttinterp}[1]{\mathcal{E}\interp{#1}}
1\newcommand{\ttinterp}[1]{\mathcal{E}\interp{#1}}
2+\newcommand{\ttpinterp}[1]{\mathcal{P}\interp{#1}}
2\newcommand{\uninterp}[1]{\mathcal{T}\interp{#1}}
3\newcommand{\uninterp}[1]{\mathcal{T}\interp{#1}}
3
4
4\section{Elaborating \Idris{}}
5\section{Elaborating \Idris{}}
@@ -86,6 +87,12 @@ instantiating one such variable also instantiates all of its dependencies.
86
87
87\label{sect:prfstate}
88\label{sect:prfstate}
88
89
90+We will build expressions in \TT{} from high level \Idris{} expressions by
91+creating a top level hole binding with the appropriate type, and refining it
92+according to the structure of the \Idris{} expression. Noting the Curry-Howard
93+correspondence between programs and proofs, and the relationship between our
94+method for elaboration and tactic
95+based theorem proving, we refer to a development as a \emph{proof}.
89A proof state is a tuple, $(\vC, \Delta, \ve, \vP, \vQ)$, containing:
96A proof state is a tuple, $(\vC, \Delta, \ve, \vP, \vQ)$, containing:
90
97
91\begin{itemize}
98\begin{itemize}
@@ -111,20 +118,28 @@ a new proof state, with an empty proof term, and using the high level definition
111to direct the building of a final proof state, with a complete proof term.
118to direct the building of a final proof state, with a complete proof term.
112
119
113In the implementation, the proof state is captured in an elaboration monad,
120In the implementation, the proof state is captured in an elaboration monad,
114−\texttt{Elab}, which is simply a state monad with exceptions. This monad
121+\texttt{Elab}, which is simply a state monad with exceptions.
122+Exceptions are captured by the option type $\MO{Result}$ which
123+either indicates success, or reports an error $\MO{Err}$:
124+
125+\DM{
126+\RW{data}\:\MO{Result}\:\vt\:=\:\MO{Success}\:\vt\:\mid\:\MO{Failure}\:\MO{Err}
127+}
128+
115−supports various operations for querying and updating the proof state,
129+The \texttt{Elab} monad supports various operations for querying and updating
116−manipulating terms, generating fresh names, etc. However, we will describe
130+the proof state, manipulating terms, generating fresh names, etc. However, we
117−\Idris{} elaboration in terms of meta-operations on the proof state, in order
131+will describe \Idris{} elaboration in terms of meta-operations on the proof
118−to capture the essence of the elaboration process without being distracted by
132+state, in order to capture the essence of the elaboration process without being
119−implementation details. These meta-operations include:
133+distracted by implementation details. These meta-operations include:
120
134
121\begin{itemize}
135\begin{itemize}
122\item \demph{Queries} which retrieve values from the proof state, without modifying
136\item \demph{Queries} which retrieve values from the proof state, without modifying
123the state. For example, we can:
137the state. For example, we can:
124\begin{itemize}
138\begin{itemize}
125\item Get the type of the current sub goal ($\MO{Type}$).
139\item Get the type of the current sub goal ($\MO{Type}$).
140+\item Get the current proof term ($\MO{Term}$).
126−\item Retrieve the local context $\Gamma$ at the current sub goal.
141+\item Retrieve the local context $\Gamma$ at the current sub goal
127−($\MO{Context}$)
142+($\MO{Context}$).
128\item Type check ($\MO{Check}$) or normalise ($\MO{Normalise}$) a term relative
143\item Type check ($\MO{Check}$) or normalise ($\MO{Normalise}$) a term relative
129to $\Gamma$.
144to $\Gamma$.
130\end{itemize}
145\end{itemize}
@@ -164,90 +179,126 @@ updates the proof state to:
164(\vC, \Delta, \hole{\vx}{\vt}\SC\vx, \langle\rangle, \langle\vx\rangle)
179(\vC, \Delta, \hole{\vx}{\vt}\SC\vx, \langle\rangle, \langle\vx\rangle)
165}
180}
166
181
167−
182+Resetting the term and queue but retaining pattern bindings
168−This allows the elaborator to use pattern bindings from the left
183+allows the elaborator to use pattern bindings from the left
169hand side of a pattern matching definition in the term on the right hand side.
184hand side of a pattern matching definition in the term on the right hand side.
170
185
171−\subsection{System State}
172−
173−\label{sect:sysstate}
174−
175−The system state is a tuple, $(\vC,\vA,\vI)$, containing:
176−
177−\begin{itemize}
178−\item A global context, $\vC$, containing pattern matching definitions and their types
179−\item Implicit arguments, $\vA$, recording which arguments are implicit for each global name
180−\item Type class instances, $\vI$, containing dictionaries for type classes
181−\end{itemize}
182−
183−In the implementation, the system state is captured in a monad, \texttt{Idris},
184−which also wraps the current proof state. This monad
185−includes additional information such as syntax overloadings,
186−command line options, and optimisations, which do not concern us here. Elaboration
187−of expressions requires access to the system state in particular in order to expand
188−implicit arguments and resolve type classes.
189−
190−For each global name, $\vA$ records whether its arguments are explicit, implicit,
191−or type class constraints. For example, recall the declaration
192−of \texttt{vAdd}:
193−
194−\begin{SaveVerbatim}{vAddImpT}
195−
196−vAdd : Num a => Vect a n -> Vect a n -> Vect a n
197−
198−\end{SaveVerbatim}
199−\useverb{vAddImpT}
200−
201−\noindent
202−Written in full, and giving each argument an explicit name, we get the
203−type declaration:
204−
205−\begin{SaveVerbatim}{vAddImpT}
206−
207−vAdd : (a : _) -> (n : _) -> (c : Num a) ->
208− (xs : Vect a n) -> (ys : Vect a n) -> Vect a n
209−
210−\end{SaveVerbatim}
211−\useverb{vAddImpT}
212−
213−\noindent
214−For \tFN{vAdd}, the state records that \texttt{a} and \texttt{n} are implicit,
215−\texttt{c} is a constraint, and \texttt{xs} and \texttt{ys} are explicit. When
216−the elaborator encounters an application of \tFN{vAdd}, it knows that unless these arguments
217−are given explicitly, the application must be expanded.
218
186
219\newcommand{\Check}{\MO{Check}_\Gamma}
187\newcommand{\Check}{\MO{Check}_\Gamma}
220\newcommand{\Eval}{\MO{Normalise}_\Gamma}
188\newcommand{\Eval}{\MO{Normalise}_\Gamma}
221\newcommand{\Unify}{\MO{Unify}_\Gamma}
189\newcommand{\Unify}{\MO{Unify}_\Gamma}
222\newcommand{\PrimUnify}{\MO{PrimUnify}_\Gamma}
190\newcommand{\PrimUnify}{\MO{PrimUnify}_\Gamma}
191+\newcommand{\Subst}{\MO{Subst}}
223
192
224\subsection{Tactics}
193\subsection{Tactics}
225
194
226% Meta-operations Check, Normalise, Unify
195% Meta-operations Check, Normalise, Unify
227In order to build \TT{} expressions from \Idris{} programs, we define a collection
196In order to build \TT{} expressions from \Idris{} programs, we define a collection
228of meta-operations for querying and modifying the proof state. Meta-operations
197of meta-operations for querying and modifying the proof state. Meta-operations
229−may have side-effects including failure, or updating the proof state. We have the following
198+may have side-effects including failure, or updating the proof state, and
230−primitive meta-operations:
199+may fail. We have the following primitive meta-operations:
231
200
232\begin{itemize}
201\begin{itemize}
233\item $\MO{Get}$ and $\MO{Put}$ which allow direct access to the proof state.
202\item $\MO{Get}$ and $\MO{Put}$ which allow direct access to the proof state.
234% Focus and Unfocus don't need to be primitive, we could show in terms of
203% Focus and Unfocus don't need to be primitive, we could show in terms of
235% Get and Put
204% Get and Put
236−\item $\MO{Focus}\:\vn$, which moves $\vn$ to the head of the hole queue.
205+%\item $\MO{Focus}\:\vn$, which moves $\vn$ to the head of the hole queue.
237−\item $\MO{Unfocus}$, which moves the current hole to the back of the hole queue.
206+%\item $\MO{Unfocus}$, which moves the current hole to the back of the hole queue.
238\item $\Check\:\ve$, which type checks an expression $\ve$ relative to a context
207\item $\Check\:\ve$, which type checks an expression $\ve$ relative to a context
239$\Gamma$, returning its type.
208$\Gamma$, returning its type.
240$\MO{Check}$ will fail
209$\MO{Check}$ will fail
241if the expression is not well-typed.
210if the expression is not well-typed.
242\item $\Eval\:\ve$, which evaluates a well-typed expression $\ve$ relative to a context
211\item $\Eval\:\ve$, which evaluates a well-typed expression $\ve$ relative to a context
243−$\Gamma$, returning its normal form.
212+$\Gamma$, returning its normal form. $\MO{Normalise}$ reduces under binders,
213+but does not reduce definitions which are not total.
244\item
214\item
245$\PrimUnify\:\ve_1\:\ve_2$,
215$\PrimUnify\:\ve_1\:\ve_2$,
246which attempts to unify $\ve_1$ and $\ve_2$ by finding the values with which
216which attempts to unify $\ve_1$ and $\ve_2$ by finding the values with which
247holes must be instantiated for $\ve_1$ and $\ve_2$ to be convertible relative
217holes must be instantiated for $\ve_1$ and $\ve_2$ to be convertible relative
248to $\Gamma$ (i.e. for $\Gamma\proves\ve_1\converts\ve_2$ to hold).
218to $\Gamma$ (i.e. for $\Gamma\proves\ve_1\converts\ve_2$ to hold).
219+\item $\Subst\:\vx\:\ve$, which instantiates a hole $\vx$ directly with a term
220+$\ve$, typically arising from unification.
249\end{itemize}
221\end{itemize}
250
222
223+\noindent
224+Additionally, we have $\MO{Focus}$, $\MO{Unfocus}$ and $\MO{NewHole}$
225+operations which manipulate
226+the hole queue. $\MO{Focus}\:\vn$ moves a hole to the head of the hole queue:
227+
228+\DM{
229+\MO{Focus}\:\vn\:=\:\RW{do}\:\AR{
230+(\vC,\Delta,\vP,\vQ;\vn;\vQ')\gets\MO{Get}\\
231+\MO{Put}\:(\vC,\Delta,\vP,\vn;\vQ;\vQ')\\
232+}
233+}
234+
235+\noindent
236+Correspondingly, $\MO{Unfocus}$ moves the currently in focus hole to the
237+end of the hole queue:
238+
239+\DM{
240+\MO{Unfocus}\:=\:\RW{do}\:\AR{
241+(\vC,\Delta,\vP,\vn;\vQ)\gets\MO{Get}\\
242+\MO{Put}\:(\vC,\Delta,\vP,\vQ;\vn)\\
243+}
244+}
245+
246+\noindent
247+$\MO{NewHole}$ declares that a new hole has been created and is to be solved
248+next:
249+
250+\DM{
251+\MO{NewHole}\:=\:\RW{do}\:\AR{
252+(\vC,\Delta,\vP,\vQ)\gets\MO{Get}\\
253+\MO{Put}\:(\vC,\Delta,\vP,\vn;\vQ)\\
254+}
255+}
256+
257+\noindent
258+\demph{Tactics} are meta-operations which operate on the sub-term given by the
259+hole at the head of the hole queue in the proof state. They take the following
260+form:
261+
262+\DM{
263+\PA{\A\A}{
264+\MO{Tactic}_\Gamma & \:\vec{\VV{args}} & \:\vt & \MoRet{\vt'}
265+}
266+}
267+
268+A tactic takes a sequence of zero or more arguments $\vec{\VV{args}}$ followed
269+by the sub-term $\vt$ on which it is operating. It runs relative to a context
270+$\Gamma$ which contains all the bindings and pattern bindings in scope at that
271+point in the term. The sub-term $\vt$ will either be a hole binding
272+$\hole{\vx}{\vT}\SC\ve$ or a guess binding $\guess{\vx}{\vT}{\vv}\SC\ve$. The
273+tactic returns a new term $\vt'$ which can take any form, provided it is
274+well-typed, with a type convertible to the type of $\vt$.
275+Tactics may also have the side effect of updating the proof state,
276+therefore we will describe tactics in a pseudo-code with $\RW{do}$ notation.
277+
278+Tactics are executed by a higher level meta-operation $\MO{RunTac}$,
279+given in Figure \ref{runtac} which
280+locates the appropriate sub-term, applies the tactic with the context
281+local to this sub-term, and
282+replaces the sub-term with the term returned by the
283+tactic.
284+% It then updates the hole queue in the proof state, and updates holes which have
285+% been solved by unification. If the tactic creates new holes, these are automatically
286+% added to the \remph{head} of the hole queue.
287+
288+\FFIG{
289+\mbox{\textbf{RunTac definition goes here}}
290+}
291+{Applying tactics to the proof state}
292+{runtac}
293+
294+In the remainder of this section, we will define a set of primitive tactics:
295+$\MO{Claim}$, $\MO{Fill}$ and $\MO{Solve}$ which are used to create and destroy
296+holes; and $\MO{Lambda}$, $\MO{Pi}$, $\MO{Let}$ and $\MO{Attack}$ which are
297+used to create binders. First, however, we must explain how unification is
298+applied during elaboration.
299+
300+\subsubsection{Unification}
301+
251Unification is crucial to the success of elaboration --- it is unification
302Unification is crucial to the success of elaboration --- it is unification
252which finds values of implicit arguments, in particular. We do not require
303which finds values of implicit arguments, in particular. We do not require
253unification to succeed immediately, in general, as instantiating a hole may
304unification to succeed immediately, in general, as instantiating a hole may
@@ -255,11 +306,11 @@ affect the solution to other unification problems. Instead,
255$\MO{PrimUnify}$ may give one of the following results:
306$\MO{PrimUnify}$ may give one of the following results:
256
307
257\begin{itemize}
308\begin{itemize}
309+\item $\MO{Success}\:(\tx=\te, \tp)$, containing the solved holes mapping
310+$\vx$ to $\ve$, and a possibly empty set of blocked unification problems $\tp$.
258−\item $\MO{Success}$, if there is a solution for $\ve_1$ and $\ve_2$.
311+If $\tp$ is empty, there is a solution for $\ve_1$ and $\ve_2$.
259−\item $\MO{Failure}$, if no solution is found due to a failure to unify
312+\item $\MO{Failure}\:\ve$, if no solution is found due to a failure to unify
260sub-terms in normal form.
313sub-terms in normal form.
261−\item $\MO{Blocked}$, otherwise, in which case further elaboration may lead
262−to further refinement of the problem.
263\end{itemize}
314\end{itemize}
264
315
265$\MO{Unify}$ attempts to solve the given unification problem using
316$\MO{Unify}$ attempts to solve the given unification problem using
@@ -275,28 +326,6 @@ state. This operation is given in Figure \ref{unifyps}.
275{The $\MO{Unify}$ meta-operation}
326{The $\MO{Unify}$ meta-operation}
276{unifyps}
327{unifyps}
277
328
278−\demph{Tactics} are meta-operations which operate on the sub-term given
279−by the hole at the head of the hole queue in the proof state. They take the following form:
280−
281−\DM{
282−\PA{\A\A}{
283−\MO{Tactic}_\Gamma & \:\vec{\VV{args}} & \:\vt & \MoRet{\vt'}
284−}
285−}
286−
287−A tactic takes a sequence of zero or more arguments $\vec{\VV{args}}$ followed
288−by the sub-term $\vt$ on which it is operating. It runs relative to a context
289−$\Gamma$ which contains all the bindings and pattern bindings in scope at that
290−point in the term. The sub-term $\vt$ will either be a hole binding
291−$\hole{\vx}{\vT}\SC\ve$ or a guess binding $\guess{\vx}{\vT}{\vv}\SC\ve$. The
292−tactic returns a new term $\vt'$ which can take any form, provided it is
293−well-typed, with a type convertible to the type of $\vt$.
294−Tactics may also have the side effect of updating the proof state,
295−therefore we will describe tactics in a pseudo-code with $\RW{do}$ notation.
296−
297−We define a set of primitive tactics: $\MO{Claim}$, $\MO{Fill}$ and $\MO{Solve}$
298−which are used to create and destroy holes; and $\MO{Lambda}$, $\MO{Pi}$, $\MO{Let}$
299−and $\MO{Attack}$ which are used to create binders.
300
329
301\subsubsection{Creating and destroying holes}
330\subsubsection{Creating and destroying holes}
302
331
@@ -448,17 +477,56 @@ Each of these tactics require the term in focus to be of the form $\hole{\vx}{\v
448This is important, because if the scope of the binding were an arbitrary expression $\ve$,
477This is important, because if the scope of the binding were an arbitrary expression $\ve$,
449the binder would be scoped across this \emph{whole} expression rather than the subexpression
478the binder would be scoped across this \emph{whole} expression rather than the subexpression
450$\vx$ as intended.
479$\vx$ as intended.
451−The $\MO{attack}$ tactic ensures that a hole
480+The $\MO{Attack}$ tactic ensures that a hole
452−is in the appropriate form, creating a new hole $\vh$ which is placed at the head
481+is in the appropriate form, creating a new hole $\vx'$ which is placed at the
453−of the queue:
482+head of the queue:
454
483
455\DM{
484\DM{
456\PA{\A}{
485\PA{\A}{
457\MO{Attack}_\Gamma & (\hole{\vx}{\vT}\SC\ve) &
486\MO{Attack}_\Gamma & (\hole{\vx}{\vT}\SC\ve) &
487+ \MoRet{\RW{do}\:\AR{
488+ \MO{NewHole}\;\vx'\\
458− \MoRet{\RW{return}\:\guess{\vx}{\vT}{(\hole{\vh}{\vT}\SC\vh)}\SC\ve}
489+ \RW{return}\;\guess{\vx}{\vT}{(\hole{\vx'}{\vT}\SC\vx')}\SC\ve}}
490+}
491+}
492+
493+\noindent
494+For example, consider the following proof term:
495+
496+\DM{
497+\hole{\vx}{\va\to\vb}\SC\vx\:\ve
498+}
499+
500+\noindent
501+If we try to instantiate $\vx$ with a $\lambda$-binding $\vn$ immediately,
502+without applying $\MO{Attack}$ first, the $\lambda$ will scope over the
503+entirety of $\vx\:\ve$, and the resulting term is not well-typed:
504+
505+\DM{
506+\AR{
507+\lam{\vn}{\va}\SC\hole{\vx}{\vb}\SC\vx\:\ve
459}
508}
460}
509}
461
510
511+\noindent
512+If, on the other hand, we $\MO{Attack}$ the hole then instantiate it with
513+a $\lambda$ binding $\vn$, we get:
514+
515+\DM{
516+\begin{array}{ll}
517+\guess{\vx}{\va\to\vb}{\hole{\vx'}{\va\to\vb}\SC\vx'}\SC\vx\:\ve
518+& \hg\mbox{(after $\MO{Attack}$)}\\
519+\guess{\vx}{\va\to\vb}{\lam{\vn}{\va}\SC\hole{\vx'}{\vb}\SC\vx'}\SC\vx\:\ve
520+& \hg\mbox{(after $\MO{Lambda}\:\vn$)}
521+\end{array}
522+}
523+
524+\noindent
525+Now, once $\vx'$ has been instantiated, the guess for $\vx$ can safely be
526+closed with $\MO{Solve}$.
527+
528+\subsubsection{Pattern Binders}
529+
462Finally, we can convert a hole binding to a pattern binding by giving the
530Finally, we can convert a hole binding to a pattern binding by giving the
463pattern variable a name. This solves a hole
531pattern variable a name. This solves a hole
464by adding the pattern binding to the proof state, and updating the proof term
532by adding the pattern binding to the proof state, and updating the proof term
@@ -481,14 +549,8 @@ building the right hand side.
481
549
482\subsubsection{Example}
550\subsubsection{Example}
483
551
484−Tactics are executed by a higher level meta-operation $\MO{RunTac}$, which
552+To illustrate how tactics are applied, using $\MO{RunTac}$, to build a
485−locates the appropriate sub-term, applies the tactic with the context
486−local to this sub-term, and
487−replaces the sub-term with the term returned by the
488−tactic. It then updates the hole queue in the proof state, and updates holes which have
489−been solved by unification. If the tactic creates new holes, these are automatically
490−added to the \remph{head} of the hole queue.
491−For example, consider the following simple
553+complete term by refining a proof state, let us consider the following
492\TT{} definition for the identify function:
554\TT{} definition for the identify function:
493
555
494\DM{
556\DM{
@@ -498,10 +560,11 @@ For example, consider the following simple
498}
560}
499}
561}
500
562
501−We can build $\FN{id}$ either as a complete term, or by applying a sequence of tactics.
563+We can build $\FN{id}$ either as a complete term, or by applying a sequence of
502−To achieve this, we create a proof state initialised with the type of $\FN{id}$ and
564+tactics. To achieve this, we create a proof state initialised with the type of
503−apply a series of $\MO{Lambda}$ and $\MO{Fill}$ operations using $\MO{RunTac}$.
565+$\FN{id}$ and apply a series of $\MO{Lambda}$ and $\MO{Fill}$ operations using
504−Note that the types on each $\MO{Lambda}$ are solved by unification:
566+$\MO{RunTac}$. Note that the types on each $\MO{Lambda}$ are taken from
567+the goal type.
505
568
506\DM{
569\DM{
507\AR{
570\AR{
@@ -521,14 +584,135 @@ Note that the types on each $\MO{Lambda}$ are solved by unification:
521}
584}
522}
585}
523
586
587+\FFIG{
588+\begin{array}{lll}
589+\mbox{Initial state}
590+& \hole{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}\SC\vx
591+& \langle\vx\rangle \\
592+%
593+\MO{Attack} &
594+\underline{
595+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
596+\hole{\vh_0}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}\SC\vh_0
597+}
598+\SC\vx
599+}
600+&
601+\langle\vh_0,\vx\rangle\\
602+%
603+\MO{Lambda}\:\vA &
604+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
605+\underline{
606+\lam{\vA}{\Type}\SC\hole{\vh_0}{\all{\va}{\vA}\SC\vA}\SC\vh_0
607+}
608+}
609+\SC\vx
610+&
611+\langle\vh_0,\vx\rangle\\
612+%
613+\MO{Attack} &
614+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
615+\lam{\vA}{\Type}\SC
616+\underline{
617+\guess{\vh_0}{\all{\va}{\vA}\SC\vA}
618+{\hole{\vh_1}{\all{\va}{\vA}\SC\vA}\SC\vh_1}
619+\SC\vh_0
620+}
621+}
622+\SC\vx
623+&
624+\langle\vh_1,\vh_0,\vx\rangle\\
625+%
626+\MO{Lambda}\:\va &
627+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
628+\lam{\vA}{\Type}\SC
629+\guess{\vh_0}{\all{\va}{\vA}\SC\vA}
630+{
631+\underline{
632+\lam{\va}{\vA}\SC\hole{\vh_1}{\vA}\SC\vh_1
633+}
634+}
635+\SC\vh_0
636+}
637+\SC\vx
638+&
639+\langle\vh_1,\vh_0,\vx\rangle\\
640+%
641+\MO{Fill}\:\va &
642+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
643+\lam{\vA}{\Type}\SC
644+\\
645+&
646+\hg\hg\hg\hg\hg\hg\hg\guess{\vh_0}{\all{\va}{\vA}\SC\vA}
647+{
648+\lam{\va}{\vA}\SC
649+\underline{
650+\guess{\vh_1}{\vA}{\va}\SC\vh_1
651+}
652+}
653+\SC\vh_0
654+}
655+\SC\vx
656+&
657+\langle\vh_1,\vh_0,\vx\rangle\\
658+%
659+\MO{Solve} &
660+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
661+\lam{\vA}{\Type}\SC
662+\guess{\vh_0}{\all{\va}{\vA}\SC\vA}
663+{
664+\lam{\va}{\vA}\SC
665+\underline{
666+\va
667+}
668+}
669+\SC\vh_0
670+}
671+\SC\vx
672+&
673+\langle\vh_0,\vx\rangle\\
674+%
675+\MO{Solve} &
676+\guess{\vx}{\all{\vA}{\Type}\SC\all{\va}{\vA}\SC\vA}{
677+\lam{\vA}{\Type}\SC
678+\underline{
679+\lam{\va}{\vA}\SC
680+\va
681+}
682+}
683+\SC\vx
684+&
685+\langle\vx\rangle\\
686+%
687+\MO{Solve} &
688+\underline{
689+\lam{\vA}{\Type}\SC
690+\lam{\va}{\vA}\SC
691+\va
692+}
693+&
694+\langle\rangle\\
695+
696+\end{array}
697+}
698+{Proof state development for $\MO{MkId}$}
699+{mkidrefine}
700+
701+Figure \ref{mkidrefine} shows how each step of this sequence of tactics affects
702+the proof state and hole queue. In each step, the underlined fragment of the
703+term is the fragment in focus. One might observe that the
704+$\MO{Attack}$/$\MO{Solve}$ pairs may be unnecessary in this case, as the term
705+is already in a form suitable for introduction. However, we retain these for
706+uniform treatment of introductions.
707+
524To aid readability, we will elide $\MO{RunTac}$, and use a semi-colon to indicate
708To aid readability, we will elide $\MO{RunTac}$, and use a semi-colon to indicate
525sequencing --- in the implementation we use wrapper functions for each tactic to
709sequencing --- in the implementation we use wrapper functions for each tactic to
526apply $\MO{RunTac}$.
710apply $\MO{RunTac}$.
527Using this convention, we can build $\FN{id}$'s type and definition as shown
711Using this convention, we can build $\FN{id}$'s type and definition as shown
528in Figure \ref{idelab}. Note that $\MO{Term}$ retrieves the proof term from the current proof
712in Figure \ref{idelab}. Note that $\MO{Term}$ retrieves the proof term from the current proof
529state. Both $\MO{MkIdType}$ and $\MO{MkId}$ finish by returning a completed \TT{} term.
713state. Both $\MO{MkIdType}$ and $\MO{MkId}$ finish by returning a completed \TT{} term.
530−Note in particular that each $\MO{Attack}$ and each $\MO{Fill}$, which create new guesses,
714+Note in particular that each tactic which introduces a guess ($\MO{Fill}$
531−are closed with a $\MO{Solve}$.
715+and $\MO{Solve}$ is closed with a $\MO{Solve}$).
532
716
533Setting up elaboration in this way, with a proof state captured in a monad,
717Setting up elaboration in this way, with a proof state captured in a monad,
534and a primitive collection of tactics,
718and a primitive collection of tactics,
@@ -583,6 +767,56 @@ a Coq proof script.
583%\DM{
767%\DM{
584%}
768%}
585
769
770+\subsection{System State}
771+
772+\label{sect:sysstate}
773+
774+To elaborate \Idris{} programs we will build expressions from high level
775+declarations of functions, data types and classes, and add the resulting
776+definitions to a global system state.
777+The system state is a tuple, $(\vC,\vA,\vI)$, containing:
778+
779+\begin{itemize}
780+\item A global context, $\vC$, containing pattern matching definitions and their types
781+\item Implicit arguments, $\vA$, recording which arguments are implicit for each global name
782+\item Type class information, $\vI$, containing method signatures and dictionaries for type classes
783+\end{itemize}
784+
785+In the implementation, the system state is captured in a monad, \texttt{Idris}.
786+This monad includes additional information such as syntax overloadings,
787+command line options, and optimisations, which do not concern us here. Elaboration
788+of expressions requires access to the system state in particular in order to expand
789+implicit arguments and resolve type classes.
790+
791+For each global name, $\vA$ records whether its arguments are explicit, implicit,
792+or type class constraints. For example, recall the declaration
793+of \texttt{vAdd}:
794+
795+\begin{SaveVerbatim}{vAddImpT}
796+
797+vAdd : Num a => Vect a n -> Vect a n -> Vect a n
798+
799+\end{SaveVerbatim}
800+\useverb{vAddImpT}
801+
802+\noindent
803+Written in full, and giving each argument an explicit name, we get the
804+type declaration:
805+
806+\begin{SaveVerbatim}{vAddImpT}
807+
808+vAdd : (a : _) -> (n : _) -> (c : Num a) ->
809+ (xs : Vect a n) -> (ys : Vect a n) -> Vect a n
810+
811+\end{SaveVerbatim}
812+\useverb{vAddImpT}
813+
814+\noindent
815+For \tFN{vAdd}, the state records that \texttt{a} and \texttt{n} are implicit,
816+\texttt{c} is a constraint, and \texttt{xs} and \texttt{ys} are explicit. When
817+the elaborator encounters an application of \tFN{vAdd}, it knows that unless these arguments
818+are given explicitly, the application must be expanded.
819+
586\subsection{Elaborating Expressions}
820\subsection{Elaborating Expressions}
587
821
588\newcommand{\piimp}[2]{\mbox{\texttt{\{ $#1$ : $#2$ \} -> }}}
822\newcommand{\piimp}[2]{\mbox{\texttt{\{ $#1$ : $#2$ \} -> }}}
@@ -611,10 +845,10 @@ a Coq proof script.
611\begin{array}{rll@{\hg}rll}
845\begin{array}{rll@{\hg}rll}
612\ve ::= & \vc & (\mbox{constant}) &
846\ve ::= & \vc & (\mbox{constant}) &
613\mid & \vx & (\mbox{variable}) \\
847\mid & \vx & (\mbox{variable}) \\
614−\mid & \vt & (\mbox{type}) &
615−\mid & \ve\:\ta & (\mbox{function application}) \\
616\mid & \ilam{\vx}\:\ve & (\mbox{lambda abstraction}) &
848\mid & \ilam{\vx}\:\ve & (\mbox{lambda abstraction}) &
849+\mid & \ve\:\ta & (\mbox{function application}) \\
617−\mid & \ilet{\vx}{\ve}\:\ve & (\mbox{let binding}) \\
850+\mid & \ilet{\vx}{\ve}\:\ve & (\mbox{let binding}) &
851+\mid & \piexp{\vx}{\vt}\vt & (\mbox{explicit function space}) \\
618%\mid & \icase\:\ve\:\iof\:\vec{\VV{alt}} & (\mbox{case expression}) &
852%\mid & \icase\:\ve\:\iof\:\vec{\VV{alt}} & (\mbox{case expression}) &
619%\mid & \mvar{\vx} & (\mbox{metavariable}) \\
853%\mid & \mvar{\vx} & (\mbox{metavariable}) \\
620\mid & \_ & (\mbox{placeholder})
854\mid & \_ & (\mbox{placeholder})
@@ -627,11 +861,6 @@ a Coq proof script.
627\end{array}
861\end{array}
628\medskip\\
862\medskip\\
629\begin{array}{rll}
863\begin{array}{rll}
630−\vt ::= & \ve & (\mbox{expression}) \\
631−\mid & \piexp{\vx}{\vt}\vt & (\mbox{explicit function space}) \\
632−\end{array}
633−\medskip\\
634−\begin{array}{rll}
635\VV{declty} ::= & \ve & (\mbox{expression}) \\
864\VV{declty} ::= & \ve & (\mbox{expression}) \\
636\mid & \piexp{\vx}{\vt}\VV{declty} & (\mbox{explicit function space}) \\
865\mid & \piexp{\vx}{\vt}\VV{declty} & (\mbox{explicit function space}) \\
637\mid & \piimp{\vx}{\vt}\VV{declty} & (\mbox{implicit function space}) \\
866\mid & \piimp{\vx}{\vt}\VV{declty} & (\mbox{implicit function space}) \\
@@ -707,14 +936,21 @@ than one at a time.
707%Implicit and type class arguments? Expanded at the application site (we need to know
936%Implicit and type class arguments? Expanded at the application site (we need to know
708%it's the global name after all and we do that by type).
937%it's the global name after all and we do that by type).
709
938
710−To elaborate expressions, we define a meta-operation $\ttinterp{\cdot}$ which
939+To elaborate expressions, we define meta-operations $\ttinterp{\cdot}$ and
711−runs relative to a proof state (see Section \ref{sect:prfstate}). Its effect is
940+$\ttpinterp{\cdot}$, which run relative to a proof state (see Section
941+\ref{sect:prfstate}). $\ttpinterp{\cdot}$ elaborates \remph{patterns}, i.e.
942+expressions on the left hand side of a pattern matching definition, whereas
943+$\ttinterp{\cdot}$ elaborates expressions on the right hand side.
944+Their effect is
712to update the proof state so that the hole in focus contains a representation
945to update the proof state so that the hole in focus contains a representation
713of the given expression, by applying tactics. We assume that the proof state
946of the given expression, by applying tactics. We assume that the proof state
714has already been set up, which means that elaboration can always be
947has already been set up, which means that elaboration can always be
715\remph{type-directed} since the proof state contains the type of the expression we
948\remph{type-directed} since the proof state contains the type of the expression we
716are building.
949are building.
717
950
951+In all cases except the elaboration of variables and constantes,
952+$\ttpinterp{\vx}\:=\:\ttinterp{\vx}$.
953+
718\subsubsection{Elaborating variables and constants}
954\subsubsection{Elaborating variables and constants}
719
955
720In the simplest cases, there is a direct translation from an \IdrisM{} expression to
956In the simplest cases, there is a direct translation from an \IdrisM{} expression to
@@ -728,19 +964,22 @@ the \MO{Fill} tactic:
728}
964}
729}
965}
730
966
967+Since $\MO{Fill}$ creates a guess binding, we use $\MO{Solve}$ to instantiate
731−We need not concern ourselves with type checking variables or constants here --- $\MO{Fill}$
968+the guess. We need not concern ourselves with type checking variables or
732−will handle this, type checking $\vx$ or $\vc$ and unifying the result with the
969+constants here --- $\MO{Fill}$ will handle this, type checking $\vx$ or $\vc$
970+and unifying the result with the hole type. If there are any errors,
733−hole type. If there are any errors, elaboration will fail.
971+elaboration will fail.
734
972
735−If we are building the left hand side of a pattern clause, however, there is a problem,
973+If we are building the left hand side of a pattern clause, however, there is a
736−as it is the left hand side which \remph{defines} variables. In this context, we assume
974+problem, as it is the left hand side which \remph{defines} variables. In this
737−that attempting to elaborate a variable which does not type check means that the variable
975+context, we assume that attempting to elaborate a variable which does not type
738−is a pattern variable:
976+check means that the variable is a pattern variable:
739
977
740\DM{
978\DM{
741−\ttinterp{\vx}\:\mq\:
979+\ttpinterp{\vx}\:\mq\:
742\MO{Try}\:\AR{(\RW{do}\:\MO{Fill}\:\vx;\:\MO{Solve})\\
980\MO{Try}\:\AR{(\RW{do}\:\MO{Fill}\:\vx;\:\MO{Solve})\\
743−(\MO{Pat}\:\vx)\hg\hg\mbox{(If elaborating a left hand side)}}
981+(\MO{Pat}\:\vx)
982+}
744}
983}
745
984
746We also need to elaborate \remph{placeholders}, which are subexpressions we expect to
985We also need to elaborate \remph{placeholders}, which are subexpressions we expect to
@@ -758,9 +997,9 @@ fails.
758
997
759\subsubsection{Elaborating bindings}
998\subsubsection{Elaborating bindings}
760
999
761−To elaborate a $\lambda$-binding, we $\MO{Attack}$ the hole, which must be a function type,
1000+To elaborate a $\lambda$-binding, we $\MO{Attack}$ the hole, then apply the
1001+$\MO{Lambda}$ tactic, which will fail if the goal is not a function type. We
762−apply the $\MO{Lambda}$ tactic, elaborate the scope, then $\MO{Solve}$, which discharges
1002+then elaborate the scope and $\MO{Solve}$, which discharges the $\MO{Attack}$:
763−the $\MO{Attack}$:
764
1003
765\DM{
1004\DM{
766\ttinterp{\ilam{\vx}\ve}\:\mq\:
1005\ttinterp{\ilam{\vx}\ve}\:\mq\:
@@ -856,30 +1095,24 @@ forms:
856}
1095}
857}
1096}
858
1097
859−The first problem has a solution: $\vf=\TC{Int}$. The second and third problems have
1098+The first problem has a solution: $\vf=\TC{Int}$. The second and third problems
860−no solution without further information. In the second case, we cannot conclude
1099+have no solution without further information. In the second case, we cannot
861−anything about $\vf$ or $\vx$ just from knowing that $\vf\:\vx\:=\:\TC{Int}$.
1100+conclude anything about $\vf$ or $\vx$ just from knowing that
862−In the third case, although we have $\vx\:=\:\vx$ in argument position, we cannot
1101+$\vf\:\vx\:=\:\TC{Int}$. In the third case, although we have $\vx\:=\:\vx$ in
863−conclude that $\vf\:=\:\vg$ unless we know that the function which solves $\vf$ or
1102+argument position, we cannot conclude that $\vf\:=\:\vg$ in general as a
864−$\vg$ is injective.
1103+result.
865−
866−Once $\vf$ or $\vg$ is solved in some other way, perhaps by being given explicitly
867−in another argument, these unification problems can make progress. Otherwise, unification
868−fails. As a result, the order in which functions and arguments are
869−elaborated matters. Sometimes, we may learn more by elaborating a function first
870−and the arguments later, sometimes it may be the other way around.
871−
872−One option when unification fails is to store the problem (i.e. the expressions to be
873−unified with their local context) and refine it with further information when it becomes
874−available. In practice, we have found a simpler approach to be effective: try
875−elaborating the function first, then arguments. If that fails, try elaborating the
876−arguments first.
877
1104
1105+Once $\vf$ or $\vg$ is solved in some other way, perhaps by being given
1106+explicitly in another argument, these unification problems can make progress.
1107+Otherwise, unification blocks. In such a case, the elaborator stores the
1108+problem (i.e. the expressions to be unified with their local context) and
1109+refines it with further information when it becomes available.
878−Elaborating a global function application makes a $\MO{Claim}$ for each argument
1110+Elaborating a global function application makes a $\MO{Claim}$ for each
879−in turn. Then the function is elaborated, applied to each of the claimed arguments.
1111+argument in turn. Then the function is elaborated, applied to each of the
880−Finally, each argument which has been given explicitly is elaborated. If this fails,
1112+claimed arguments. Each argument which has been given explicitly is
881−elaboration tries again in the opposite order. Finally, any type
1113+elaborated.
882−class instance arguments are resolved with the built-in $\MO{Instance}$ tactic.
1114+Finally, any type class instance arguments are resolved with the
1115+built-in $\MO{Instance}$ tactic.
883
1116
884\DM{
1117\DM{
885\ttinterp{\vx\:\ta}\:\mq\:
1118\ttinterp{\vx\:\ta}\:\mq\:
@@ -888,18 +1121,9 @@ class instance arguments are resolved with the built-in $\MO{Instance}$ tactic.
888\AR{
1121\AR{
889(\tn,\tv)\:\gets\:\MO{Expand}\:\vx\:\ta\\
1122(\tn,\tv)\:\gets\:\MO{Expand}\:\vx\:\ta\\
890\vec{\MO{Claim}}\:(\tn\Hab\tT)\\
1123\vec{\MO{Claim}}\:(\tn\Hab\tT)\\
891−\MO{Try}\:\AR{
892−(\RW{do}\:\AR{
893\MO{Fill}\:\vx\:\tn\\
1124\MO{Fill}\:\vx\:\tn\\
894\vec{\MO{Focus}}\:\tn;\:\ttinterp{\tv}\hg\mbox{(for non-placeholder $\vv$)}\\
1125\vec{\MO{Focus}}\:\tn;\:\ttinterp{\tv}\hg\mbox{(for non-placeholder $\vv$)}\\
895−\MO{Solve})
1126+\MO{Solve} \\
896−}\\
897−(\RW{do}\:\AR{
898−\vec{\MO{Focus}}\:\tn;\:\ttinterp{\tv}\hg\mbox{(for non-placeholder $\vv$)}\\
899−\MO{Fill}\:\vx\:\tn\\
900−\MO{Solve})
901−}}
902−\\
903\vec{\MO{Instance}}\:\tn\hg\mbox{(for type class constraint argument $\vn$)}
1127\vec{\MO{Instance}}\:\tn\hg\mbox{(for type class constraint argument $\vn$)}
904}
1128}
905}
1129}
@@ -910,27 +1134,19 @@ a type class instance which would solve the goal directly. First, it examines th
910context, then recursively searches the global context. $\MO{Instance}$ is covered
1134context, then recursively searches the global context. $\MO{Instance}$ is covered
911in detail in Section \ref{sect:instance}.
1135in detail in Section \ref{sect:instance}.
912
1136
913−To elaborate a simple function application, of an arbitrary expression to an arbitrary
1137+To elaborate a simple function application, of an arbitrary expression to an
914−argument, we need not worry about implicit arguments or class constraints. Instead,
1138+arbitrary argument, we need not worry about implicit arguments or class
915−the function and argument are elaborated, and the results applied. Since elaboration
1139+constraints. Instead, the function and argument are elaborated, and the results
1140+applied. Since elaboration is type directed, however, there must be an
916−is type directed, however, there must be an appropriate type for the function.
1141+appropriate type for the function.
917−As before, elaboration tries two orders, as it may learn something from elaborating
918−the function or argument which helps with unification:
919
1142
920\DM{
1143\DM{
921\ttinterp{\ve\:\va}\:\mq\:\RW{do}\:
1144\ttinterp{\ve\:\va}\:\mq\:\RW{do}\:
922\AR{
1145\AR{
923\MO{Claim}\:(\vA\Hab\Type);\;\MO{Claim}\:(\vB\Hab\Type)\\
1146\MO{Claim}\:(\vA\Hab\Type);\;\MO{Claim}\:(\vB\Hab\Type)\\
924\MO{Claim}\:(\vf\Hab\vA\to\vB);\;\MO{Claim}\:(\vs\Hab\vA)\\
1147\MO{Claim}\:(\vf\Hab\vA\to\vB);\;\MO{Claim}\:(\vs\Hab\vA)\\
925−\MO{Try}\:
926−\AR{
927−(\RW{do}\:\AR{
928\MO{Focus}\:\vf;\;\ttinterp{\ve}\\
1148\MO{Focus}\:\vf;\;\ttinterp{\ve}\\
929−\MO{Focus}\:\vs;\;\ttinterp{\va})}\\
930−(\RW{do}\:\AR{
931\MO{Focus}\:\vs;\;\ttinterp{\va}\\
1149\MO{Focus}\:\vs;\;\ttinterp{\va}\\
932−\MO{Focus}\:\vf;\;\ttinterp{\ve})}\\
933−}
934}
1150}
935}
1151}
936
1152
@@ -1021,7 +1237,7 @@ extracting the value and unified type when complete.
1021\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve)\:\mq\:\\
1237\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve)\:\mq\:\\
1022\hg\hg\edo{
1238\hg\hg\edo{
1023\MO{NewProof}\:\TC{Infer};\;
1239\MO{NewProof}\:\TC{Infer};\;
1024−\ttinterp{\DC{MkInfer}\:\_\:(\vx\:\ttt)}\\
1240+\ttpinterp{\DC{MkInfer}\:\_\:(\vx\:\ttt)}\\
1025\DC{MkInfer}\:\vT\:\VV{lhs}\gets\MO{Term}\\
1241\DC{MkInfer}\:\vT\:\VV{lhs}\gets\MO{Term}\\
1026\tp\gets\MO{Patterns}\\
1242\tp\gets\MO{Patterns}\\
1027\MO{NewTerm}\:\vT;\;
1243\MO{NewTerm}\:\vT;\;
@@ -1059,7 +1275,7 @@ which are then recursively elaborated before the right hand side.
1059\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve\:\iwhere\:\td)\:\mq\:\\
1275\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve\:\iwhere\:\td)\:\mq\:\\
1060\hg\hg\edo{
1276\hg\hg\edo{
1061\MO{NewProof}\:\TC{Infer};\;
1277\MO{NewProof}\:\TC{Infer};\;
1062−\ttinterp{\DC{MkInfer}\:\_\:(\vx\:\ttt)}\\
1278+\ttpinterp{\DC{MkInfer}\:\_\:(\vx\:\ttt)}\\
1063\DC{MkInfer}\:\vT\:\VV{lhs}\gets\MO{Term}\\
1279\DC{MkInfer}\:\vT\:\VV{lhs}\gets\MO{Term}\\
1064\tp\gets\MO{Patterns}\\
1280\tp\gets\MO{Patterns}\\
1065(\td', \ve') \gets\MO{Lift}\:\tp\:\td\:\ve\\
1281(\td', \ve') \gets\MO{Lift}\:\tp\:\td\:\ve\\
@@ -1201,13 +1417,13 @@ variables necessary to type $\vW$ and the rest, as before.
1201
1417
1202\subsubsection{Elaborating Class and Instance Declarations}
1418\subsubsection{Elaborating Class and Instance Declarations}
1203
1419
1204−Type classes are implemented as dictionaries of functions for each instance. Therefore,
1420+Type classes are implemented as dictionaries of functions for each instance,
1421+following the implementation of first class type classes in Coq~\cite{Sozeau2008}.
1205−a type class declaration elaborates to a record type containing all of the functions
1422+Therefore, a type class declaration elaborates to a record type containing all
1206−in the class, and an instance is simply an instance of the record. The methods
1423+of the functions in the class, and an instance is simply an instance of the
1207−are translated to top level functions which extract the relevant function from the
1424+record. The methods are translated to top level functions which extract the
1208−dictionary.
1209−For example, for the \texttt{Show} type class, the \texttt{class}
1425+relevant function from the dictionary. For example, for the \texttt{Show} type
1210−declaration is translated to:
1426+class, the \texttt{class} declaration is translated to:
1211
1427
1212\begin{SaveVerbatim}{showdata}
1428\begin{SaveVerbatim}{showdata}
1213
1429
@@ -1225,7 +1441,7 @@ An instance for a type \texttt{a} is then a value of type \texttt{Show a}; for e
1225
1441
1226\begin{SaveVerbatim}{shownatf}
1442\begin{SaveVerbatim}{shownatf}
1227
1443
1228−showNat : Show Nat;
1444+showNat : Show Nat
1229showNat = instanceShow show' where
1445showNat = instanceShow show' where
1230 show' : Nat -> String
1446 show' : Nat -> String
1231 show' O = "O"
1447 show' O = "O"
@@ -1312,15 +1528,8 @@ In general, \texttt{class} declarations are elaborated as follows:
1312(\vec{\VV{meth}}\Hab\ttt)\gets\td\\
1528(\vec{\VV{meth}}\Hab\ttt)\gets\td\\
1313\MO{Elab}\:
1529\MO{Elab}\:
1314\AR{(\RW{data}\:\TC{C}\Hab(\ta\Hab\ttt)\to\Type\:\iwhere\\
1530\AR{(\RW{data}\:\TC{C}\Hab(\ta\Hab\ttt)\to\Type\:\iwhere\\
1315−\hg\DC{InstanceC}\Hab\piconst{\tc}\td\to\TC{C}\:\ta)}
1531+\hg\DC{InstanceC}\Hab\piconst{\tc}\td\to\TC{C}\:\ta)}\\
1316−}
1317−}
1318−}
1319−
1320−\DM{
1321−\AR{
1322−\AR{
1323−\vec{\MO{ElabMeth}}\:\vec{\VV{meth}}\:\td\\
1532+\vec{\MO{ElabMeth}}\:(\vec{\VV{meth}}\Hab\ttt)\\
1324\vec{\MO{ElabParent}}\:\tc
1533\vec{\MO{ElabParent}}\:\tc
1325}
1534}
1326\medskip\\
1535\medskip\\
@@ -1339,13 +1548,16 @@ In general, \texttt{class} declarations are elaborated as follows:
1339}
1548}
1340
1549
1341\noindent
1550\noindent
1342−Then \texttt{instance} declarations are elaborated as follows:
1551+Then \texttt{instance} declarations are elaborated as follows. One complication
1552+is that the function definitions in an \texttt{instance} declaration do not
1553+have explicit types, so we must retrieve the method types from the system state
1554+and substitute $\TC{C}\:\ttt$, using $\MO{ExpandTypes}$:
1343
1555
1344\DM{
1556\DM{
1345\AR{
1557\AR{
1346\MO{Elab}\:(\iinstance\;\piconst{\tc}\TC{C}\:\ttt\:\iwhere\:\td)\\
1558\MO{Elab}\:(\iinstance\;\piconst{\tc}\TC{C}\:\ttt\:\iwhere\:\td)\\
1347\hg\hg\mq\:\RW{do}\:\AR{
1559\hg\hg\mq\:\RW{do}\:\AR{
1348−(\vec{\VV{meth}}, \vec{\VV{ty}})\gets\td\\
1560+(\vec{\VV{meth}}, \vec{\VV{ty}})\gets\MO{ExpandTypes}\:\td\\
1349\vec{\VV{ty'}}\:=\:\vec{\VV{ty}}[\ttt/\ta]\\
1561\vec{\VV{ty'}}\:=\:\vec{\VV{ty}}[\ttt/\ta]\\
1350\MO{Elab}\:(\FN{instanceC}\Hab\piconst{\tc}\TC{C}\:\ttt)\\
1562\MO{Elab}\:(\FN{instanceC}\Hab\piconst{\tc}\TC{C}\:\ttt)\\
1351\MO{Elab}\:\AR{
1563\MO{Elab}\:\AR{
papers/impl-paper/hll.tex+93−64
@@ -40,9 +40,9 @@ main = putStrLn ("The answer is " ++ show x)
40Like Haskell, the main function
40Like Haskell, the main function
41is called \texttt{main}, and input and output is managed with an \texttt{IO}
41is called \texttt{main}, and input and output is managed with an \texttt{IO}
42monad. Unlike Haskell, however, a single colon indicates \texttt{:} a type
42monad. Unlike Haskell, however, a single colon indicates \texttt{:} a type
43−declaration, emphasising the importance of types,
43+signature, emphasising the importance of types,
44−and \remph{all} functions must have a top level type
44+and \remph{all} top level functions must have a type
45−declaration. This is due to type inference being, in general,
45+signature. This is due to type inference being, in general,
46undecidable for languages with
46undecidable for languages with
47dependent types.
47dependent types.
48
48
@@ -191,14 +191,16 @@ data Vect : Type -> Nat -> Type where
191\useverb{vect}
191\useverb{vect}
192
192
193\noindent
193\noindent
194−Note that this uses the same constructor names as for \tTC{List}. Ad-hoc
194+Note that this uses the same constructor names as for \tTC{List}. Ad-hoc name
195−name overloading such as this is accepted by \Idris{}, provided that the names
195+overloading such as this is accepted by \Idris{}, provided that the names are
196−are declared in different namespaces (in practice, normally in different modules).
196+declared in different namespaces (in practice, normally in different modules)
197−Ambiguous constructor names are resolved by type.
197+so that the names are different internally. Namespace resolution can be made
198−
198+explicitly (e.g. \texttt{List.Nil} or \texttt{Vect.Nil}) or more commonly
199+by type.
200+
199−This declares a family of types, which requires a different form of declaration
201+The above declaration creates a family of types, and requires a different form
200−from the simple type declarations above. It resembles a Haskell GADT
202+of declaration from the simple type declarations above. It resembles a Haskell
201−declaration: it explicitly states the type
203+GADT declaration: it explicitly states the type
202of the type constructor \tTC{Vect} --- it takes a type and a \tTC{Nat} as an
204of the type constructor \tTC{Vect} --- it takes a type and a \tTC{Nat} as an
203argument, where \tTC{Type} stands for the type of types. We say that \tTC{Vect}
205argument, where \tTC{Type} stands for the type of types. We say that \tTC{Vect}
204is \emph{parameterised} by a type, and \emph{indexed} over \tTC{Nat}.
206is \emph{parameterised} by a type, and \emph{indexed} over \tTC{Nat}.
@@ -219,13 +221,13 @@ library, appends two \tTC{Vect}s:
219\begin{SaveVerbatim}{vapp}
221\begin{SaveVerbatim}{vapp}
220
222
221(++) : Vect A n -> Vect A m -> Vect A (n + m)
223(++) : Vect A n -> Vect A m -> Vect A (n + m)
222−(++) Nil ys = ys
224+Nil ++ ys = ys
223−(++) (x :: xs) ys = x :: xs ++ ys
225+(x :: xs) ++ ys = x :: xs ++ ys
224
226
225\end{SaveVerbatim}
227\end{SaveVerbatim}
226\useverb{vapp}
228\useverb{vapp}
227
229
228−\subsubsection{The Finite Sets}
230+\subsubsection*{Example: The Finite Sets}
229
231
230Finite sets, as the name suggests, are sets with a finite number of elements.
232Finite sets, as the name suggests, are sets with a finite number of elements.
231They are declared as follows in the library:
233They are declared as follows in the library:
@@ -273,11 +275,10 @@ need for a run-time bounds check. The type checker guarantees that the location
273is no larger than the length of the vector.
275is no larger than the length of the vector.
274
276
275Note also that there is no case for \texttt{Nil} here. It would be impossible
277Note also that there is no case for \texttt{Nil} here. It would be impossible
276−to apply such a case ---
277−since there is no element of \texttt{Fin O}, and the location is a
278+to apply such a case --- since there is no element of \texttt{Fin O}, and the
278−\texttt{Fin n}, then \texttt{n} can not be \tDC{O}. As a result, attempting to
279+location is a \texttt{Fin n}, then \texttt{n} can not be \tDC{O}. As a result,
279−look up an element in an empty vector would give a compile time type error,
280+attempting to look up an element in an empty vector would give a compile time
280−since it would force \texttt{n} to be \tDC{O}.
281+type error.
281
282
282\subsubsection{Implicit Arguments}
283\subsubsection{Implicit Arguments}
283
284
@@ -315,10 +316,10 @@ index : {a:Type} -> {n:Nat} -> Fin n -> Vect a n -> a
315\useverb{vindeximpty}
316\useverb{vindeximpty}
316
317
317\noindent
318\noindent
318−Implicit arguments, given in braces \texttt{\{\}} in the type declaration, are
319+Implicit arguments, given in braces \texttt{\{\}} in the type signature, are
319not given in applications of \texttt{index}; their values can be inferred from
320not given in applications of \texttt{index}; their values can be inferred from
320the types of the \texttt{Fin n} and \texttt{Vect a n} arguments. Any name which
321the types of the \texttt{Fin n} and \texttt{Vect a n} arguments. Any name which
321−appears as a parameter or index in a type declaration, but which is otherwise
322+appears as a parameter or index in a type signature, but which is otherwise
322free, will be automatically bound as an implicit argument. Implicit arguments
323free, will be automatically bound as an implicit argument. Implicit arguments
323can still be given explicitly in applications, using the syntax
324can still be given explicitly in applications, using the syntax
324\texttt{\{a=value\}} and \texttt{\{n=value\}}, for example:
325\texttt{\{a=value\}} and \texttt{\{n=value\}}, for example:
@@ -345,6 +346,41 @@ index : (i:Fin n) -> (xs:Vect a n) -> a
345This can be useful for improving the readability of type signatures, particularly
346This can be useful for improving the readability of type signatures, particularly
346where the name suggests the argument's purpose.
347where the name suggests the argument's purpose.
347
348
349+\subsubsection{Totality Checking}
350+
351+Internally, \Idris{} programs are checked for \emph{totality} --- that they
352+produce an answer in finite time for all possible inputs --- but they are not
353+\emph{required} to be total by default. Totality checking serves two
354+purposes: firstly, if a program terminates for all inputs
355+then its type gives a strong
356+guarantee about the properties specified by its type; secondly, we can
357+optimise total programs more aggressively~\cite{Brady2003}.
358+
359+Totality checking can be enforced by using the \texttt{total} keyword. For
360+example, recall the \texttt{vAdd} function:
361+
362+\begin{SaveVerbatim}{vadd}
363+
364+total vAdd : Num a => Vect a n -> Vect a n -> Vect a n
365+vAdd [] [] = []
366+vAdd (x :: xs) (y :: ys) = x + y :: vAdd xs ys
367+
368+\end{SaveVerbatim}
369+\useverb{vadd}
370+
371+\noindent
372+The elaborator can verify that this is total by checking that it covers all
373+possible patterns --- in this case, both arguments must be of the same form
374+as the type requires that the input vectors are the same length --- and that
375+recursive calls are on structurally smaller values. The totality checker is
376+implemented independently of the type checker and elaborator presented in the
377+remainder of this paper. While we could consider building totality proofs
378+by translating functions to eliminators~\cite{McBride2004a} we have taken
379+the more pragmatic approach of allowing more flexibility for the programmer
380+at the expense of simplicity of totality checking. \Idris{} also supports
381+coinductive definitions with a corresponding productivity checker, although
382+futher details are beyond the scope of this paper.
383+
348\subsection{Type Classes}
384\subsection{Type Classes}
349
385
350\Idris{} supports overloading in two ways. Firstly, as we have already seen with
386\Idris{} supports overloading in two ways. Firstly, as we have already seen with
@@ -476,41 +512,41 @@ class Eq a => Ord a where
476
512
477\subsection{Matching on intermediate values}
513\subsection{Matching on intermediate values}
478
514
479−\subsubsection{\texttt{let} bindings}
515+%\subsubsection{\texttt{let} bindings}
480−
516+%
481−Intermediate values can be calculated using \texttt{let} bindings:
517+%Intermediate values can be calculated using \texttt{let} bindings:
482−
518+%
483−\begin{SaveVerbatim}{letb}
519+%\begin{SaveVerbatim}{letb}
484−
520+%
485−mirror : List a -> List a
521+%mirror : List a -> List a
486−mirror xs = let xs' = rev xs in
522+%mirror xs = let xs' = rev xs in
487− app xs xs'
523+% app xs xs'
488−
524+%
489−\end{SaveVerbatim}
525+%\end{SaveVerbatim}
490−\useverb{letb}
526+%\useverb{letb}
491−
527+%
492−\noindent
528+%\noindent
493−Pattern matching is also supported in \texttt{let} bindings. For example, extracting
529+%Pattern matching is also supported in \texttt{let} bindings. For example, extracting
494−fields from a record can be achieved as follows, as well as by pattern matching at the top level:
530+%fields from a record can be achieved as follows, as well as by pattern matching at the top level:
495−
531+%
496−\begin{SaveVerbatim}{letp}
532+%\begin{SaveVerbatim}{letp}
497−
533+%
498−data Person = MkPerson String Int
534+%data Person = MkPerson String Int
499−
535+%
500−showPerson : Person -> String
536+%showPerson : Person -> String
501−showPerson p = let MkPerson name age = p in
537+%showPerson p = let MkPerson name age = p in
502− name ++ " is " ++ show age ++ " years old"
538+% name ++ " is " ++ show age ++ " years old"
539+%
540+%\end{SaveVerbatim}
541+%\useverb{letp}
503
542
504−\end{SaveVerbatim}
505−\useverb{letp}
506\subsubsection{\texttt{case} expressions}
543\subsubsection{\texttt{case} expressions}
507
544
508−Another way of inspecting intermediate values of \emph{simple} types
545+Intermediate values of \emph{non-dependent} types can be inspected using a
509−is to use a \texttt{case} expression.
510−For example, \texttt{list\_lookup}
546+\texttt{case} expression. For example, \texttt{list\_lookup} looks up an index
511−looks up an index in a list, returning \texttt{Nothing} if the index is out
547+in a list, returning \texttt{Nothing} if the index is out of bounds. This can
512−of bounds. This can be used this to write \texttt{lookup\_default}, which
548+be used this to write \texttt{lookup\_default}, which looks up an index and
513−looks up an index and returns a default value if the index is out of bounds:
549+returns a default value if the index is out of bounds:
514
550
515\begin{SaveVerbatim}{listlookup}
551\begin{SaveVerbatim}{listlookup}
516
552
@@ -522,17 +558,10 @@ lookup_default i xs def = case list_lookup i xs of
522\end{SaveVerbatim}
558\end{SaveVerbatim}
523\useverb{listlookup}
559\useverb{listlookup}
524
560
525−The \texttt{case} construct is intended for simple analysis
561+The \texttt{case} construct is intended for simple analysis of intermediate
526−of intermediate expressions to avoid the need to write auxiliary functions, and is
562+expressions to avoid the need to write auxiliary functions. It will
527−also used internally to implement pattern matching \texttt{let} and lambda bindings.
528−It will \emph{only} work if:
529−
530−\begin{itemize}
531−\item Each branch \emph{matches} a value of the same type, and \emph{returns} a
563+\emph{only} work if each branch \emph{matches} a value of the same type, and
532−value of the same type.
564+\emph{returns} a value of the same type.
533−\item The type of the result is ``known''. i.e. the type of the expression can be
534−determined \emph{without} type checking the \texttt{case}-expression itself.
535−\end{itemize}
536
565
537\subsubsection{The \texttt{with} rule}
566\subsubsection{The \texttt{with} rule}
538
567
papers/impl-paper/impldtp.tex+2−2
@@ -10,13 +10,13 @@
10\usepackage{epsfig}
10\usepackage{epsfig}
11%\usepackage{path}
11%\usepackage{path}
12\usepackage{url}
12\usepackage{url}
13−%\usepackage{amsmath,amssymb}
13+%\usepackage{amsmath}
14\usepackage{fancyvrb}
14\usepackage{fancyvrb}
15\usepackage{todonotes}
15\usepackage{todonotes}
16
16
17\newenvironment{template}{\sffamily}
17\newenvironment{template}{\sffamily}
18
18
19−\usepackage{graphics,epsfig}
19+%\usepackage{graphics,epsfig}
20\usepackage{stmaryrd}
20\usepackage{stmaryrd}
21
21
22\input{./macros.ltx}
22\input{./macros.ltx}
papers/impl-paper/intro.tex+4−4
@@ -120,10 +120,10 @@ vAdd (x :: xs) (y :: ys) = x + y :: vAdd xs ys
120This illustrates some basic features of \Idris{}:
120This illustrates some basic features of \Idris{}:
121
121
122\begin{itemize}
122\begin{itemize}
123−\item A single colon is used for type declarations, and a double colon for the
123+\item A single colon is used for type signatures, and a double colon for the
124cons operator.
124cons operator.
125−\item Functions are defined by pattern matching, with a top level type declaration.
125+\item Functions are defined by pattern matching, with a top level type signature.
126−Names which are free in the type declaration (\texttt{a} and \texttt{n}) here
126+Names which are free in the type signature (\texttt{a} and \texttt{n}) here
127are implicitly bound.
127are implicitly bound.
128\item The type system ensures that both input vectors are the same length (\texttt{n})
128\item The type system ensures that both input vectors are the same length (\texttt{n})
129and have the same element type (\texttt{a}), and that the element type and length
129and have the same element type (\texttt{a}), and that the element type and length
@@ -136,7 +136,7 @@ of the vector \texttt{a} must be numeric and therefore supports the \texttt{+} o
136\Idris{} programs elaborate into a small core language, \TT{}, which is a $\lambda$-calculus
136\Idris{} programs elaborate into a small core language, \TT{}, which is a $\lambda$-calculus
137with dependent types, augmented with algebraic data types and pattern matching.
137with dependent types, augmented with algebraic data types and pattern matching.
138\TT{} programs are fully explicitly typed, including the names \texttt{a} and \texttt{n}
138\TT{} programs are fully explicitly typed, including the names \texttt{a} and \texttt{n}
139−in the type declaration, and the names bound in each pattern match clause.
139+in the type signature, and the names bound in each pattern match clause.
140Type classes are also made explicit.
140Type classes are also made explicit.
141The \TT{} translation of \texttt{vAdd} is:
141The \TT{} translation of \texttt{vAdd} is:
142
142
papers/impl-paper/typechecking.tex+139−134
@@ -312,7 +312,8 @@ index $\vk$ is given \remph{explicitly}.
312
312
313\label{sect:patdefs}
313\label{sect:patdefs}
314
314
315−\subsubsection{Syntax}
315+%\subsubsection{Syntax}
316+
316
317
317A pattern matching definition for a function named $\FN{f}$ takes the following form,
318A 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:
319consisting of a type declaration followed by one or more pattern clauses:
@@ -347,144 +348,148 @@ clause is defined by the following rule:
347}
348}
348}
349}
349
350
350−A valid pattern matching definition effectively extends \TT{} with a new constant,
351+A valid pattern matching definition effectively extends \TT{} with a new
351−with the given type (extending the initial typing rules given in Section \ref{sect:typerules})
352+constant, with the given type (extending the initial typing rules given in
352−and reduction behaviour (extending the initial reduction rules given in Section
353+Section \ref{sect:typerules}) and reduction behaviour (extending the initial
353−\ref{sect:evaluation}).
354+reduction rules given in Section \ref{sect:evaluation}).
354−
355+Patterns are separated into the accessible patterns (variables and constructor
356+forms which may be inspected) and inaccessible patterns, following
357+Agda~\cite{norell2007thesis} then implemented by compilation into case
358+trees~\cite{Augustsson1985}.
359+
355−\subsubsection{Semantics}
360+% \subsubsection{Semantics}
356−
361+%
357−Matching on an expression proceeds by comparing the expression to
362+% Matching on an expression proceeds by comparing the expression to
358−each match clause in order, resulting in either:
363+% each match clause in order, resulting in either:
359−
364+%
360−\begin{itemize}
365+% \begin{itemize}
361−\item \demph{Success}, with pattern variables mapping to expressions
366+% \item \demph{Success}, with pattern variables mapping to expressions
362−\item \demph{Failure}, with matching continuing by proceeding to the next match clause
367+% \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
368+% \item Matching being \demph{blocked}, for example by attempting to match a variable
364−against a constructor pattern. In this case, no reduction occurs, because instantiating
369+% against a constructor pattern. In this case, no reduction occurs, because instantiating
365−the variable may provide enough information for the clause to match.
370+% the variable may provide enough information for the clause to match.
366−\end{itemize}
371+% \end{itemize}
367−
372+%
368−% Pat(x args) => x (Pat' args)
373+% % Pat(x args) => x (Pat' args)
369−% Pat (x) => x
374+% % Pat (x) => x
370−% Pat _ => _
375+% % Pat _ => _
371−
376+%
372−In order to implement pattern matching, we must separate the \remph{accessible} and
377+% In 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
378+% \remph{inaccessible} patterns. A pattern is \demph{accessible} (that is, it is possible
374−to match against it) if it is constructor headed, or a variable. Inaccessible patterns
379+% to match against it) if it is constructor headed, or a variable. Inaccessible patterns
375−are converted to ``match anything'' patterns. Clauses are converted to matchable pattern
380+% are converted to ``match anything'' patterns. Clauses are converted to matchable pattern
376−clauses with the $\MO{Clause}$ operation, given in Figure \ref{mkclause}.
381+% clauses with the $\MO{Clause}$ operation, given in Figure \ref{mkclause}.
377−We extend the vector notation to meta-operations: the notation $\vec{\MO{Pat}}$ lifts the
382+% We extend the vector notation to meta-operations: the notation $\vec{\MO{Pat}}$ lifts the
378−$\MO{Pat}$ operation across a list of arguments.
383+% $\MO{Pat}$ operation across a list of arguments.
379−
384+%
380−\FFIG{
385+% \FFIG{
381−\AR{
386+% \AR{
382−\PA{\A}{
387+% \PA{\A}{
383−& \MO{Clause} & (\pat{\tx}{\tU}\SC\FN{f}\:\tts\:=\:\ve)
388+% & \MO{Clause} & (\pat{\tx}{\tU}\SC\FN{f}\:\tts\:=\:\ve)
384− & \MoRet{\FN{f}\:(\vec{\MO{Pat}}\:\tts)\:=\:\ve}
389+% & \MoRet{\FN{f}\:(\vec{\MO{Pat}}\:\tts)\:=\:\ve}
385−}
390+% }
386−\medskip\\
391+% \medskip\\
387−\PA{\A}{
392+% \PA{\A}{
388−& \MO{Pat} & (\vx\:\tts) & \MoRet{\vx\:(\vec{\MO{Pat}}\:\tts)
393+% & \MO{Pat} & (\vx\:\tts) & \MoRet{\vx\:(\vec{\MO{Pat}}\:\tts)
389− \hg\mbox{(if $\MO{Con}\:\vx$ and $\MO{Arity}\:\vx = \MO{Length}\:\tts$)}} \\
394+% \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)\\
395+% & \MO{Pat} & \vx & \MoRet{\vx} \hg\mbox(if $\vx$ is a pattern variable)\\
391−& \MO{Pat} & \cdot & \MoRet{\_} \hg\mbox(in all other cases)\\
396+% & \MO{Pat} & \cdot & \MoRet{\_} \hg\mbox(in all other cases)\\
392−}
397+% }
393−}
398+% }
394−}
399+% }
395−{Building matchable pattern clauses}
400+% {Building matchable pattern clauses}
396−{mkclause}
401+% {mkclause}
397−
402+%
398−
403+%
399−% Match (c args, c args') => Match' (args, args')
404+% % Match (c args, c args') => Match' (args, args')
400−% Match (c args, x) => Blocked
405+% % Match (c args, x) => Blocked
401−% Match (x, t) => Success (x => t)
406+% % Match (x, t) => Success (x => t)
402−% Match (_, t) => Success ()
407+% % Match (_, t) => Success ()
403−
408+%
404−\newcommand{\Blocked}{\mathsf{Blocked}}
409+% \newcommand{\Blocked}{\mathsf{Blocked}}
405−\newcommand{\Success}{\mathsf{Success}}
410+% \newcommand{\Success}{\mathsf{Success}}
406−\newcommand{\Failure}{\mathsf{Failure}}
411+% \newcommand{\Failure}{\mathsf{Failure}}
407−
412+%
408−Pattern matching is invoked by the evaluator on encountering a function with
413+% Pattern matching is invoked by the evaluator on encountering a function with
409−a pattern matching definition applied to some arguments. If successful, pattern
414+% a pattern matching definition applied to some arguments. If successful, pattern
410−matching returns a new expression, which can then be reduced further.
415+% matching returns a new expression, which can then be reduced further.
411−Figure \ref{pmatch} gives the semantics of pattern matching a collection of
416+% Figure \ref{pmatch} gives the semantics of pattern matching a collection of
412−matchable clauses
417+% matchable clauses
413−$\tc$ against an expression $\ve$. $\MO{Match}$ attempts to match each clause in
418+% $\tc$ against an expression $\ve$. $\MO{Match}$ attempts to match each clause in
414−turn against $\ve$. If matching a clause returns $\Success$ or $\Blocked$, then
419+% turn against $\ve$. If matching a clause returns $\Success$ or $\Blocked$, then
415−matching terminates. If matching a clause returns $\Failure$, then matching proceeds
420+% matching terminates. If matching a clause returns $\Failure$, then matching proceeds
416−with the next clause. The algorithm uses the following meta-operations:
421+% with the next clause. The algorithm uses the following meta-operations:
417−
422+%
418−\begin{itemize}
423+% \begin{itemize}
419−\item $\MO{Con}\:\vx$, which returns true if $\vx$ is a constructor name
424+% \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
425+% \item $\MO{Arity}\:\vx$, which, if $\vx$ is a constructor name, returns the number of arguments
421−$\vx$ requires.
426+% $\vx$ requires.
422−\item $\MO{Length}\:\tts$, which returns the length of the vector $\tts$
427+% \item $\MO{Length}\:\tts$, which returns the length of the vector $\tts$
423−\end{itemize}
428+% \end{itemize}
424−
429+%
425−
430+%
426−\FFIG{
431+% \FFIG{
427−\AR{
432+% \AR{
428−\PA{\A\A}{
433+% \PA{\A\A}{
429−& \MO{MatchArg} & (\vx\:\tts) & (\vx\:\tts')
434+% & \MO{MatchArg} & (\vx\:\tts) & (\vx\:\tts')
430− & \MoRet{\vec{\MO{MatchArg}}\:\tts\:\tts'\hg\mbox{(if $\MO{Con}\:\vx$)}}\\
435+% & \MoRet{\vec{\MO{MatchArg}}\:\tts\:\tts'\hg\mbox{(if $\MO{Con}\:\vx$)}}\\
431−& \MO{MatchArg} & (\vx\:\tts) & \vx & \MoRet{\Blocked} \\
436+% & \MO{MatchArg} & (\vx\:\tts) & \vx & \MoRet{\Blocked} \\
432−& \MO{MatchArg} & \vx & \vt & \MoRet{\Success\:(\vx\mapsto\vt)} \\
437+% & \MO{MatchArg} & \vx & \vt & \MoRet{\Success\:(\vx\mapsto\vt)} \\
433−& \MO{MatchArg} & \_ & \vt & \MoRet{\Success\:()} \\
438+% & \MO{MatchArg} & \_ & \vt & \MoRet{\Success\:()} \\
434−& \MO{MatchArg} & \cdot & \cdot & \MoRet{\Failure}
439+% & \MO{MatchArg} & \cdot & \cdot & \MoRet{\Failure}
435−}
440+% }
436−\medskip\\
441+% \medskip\\
437−\PA{\A\A}{
442+% \PA{\A\A}{
438−& \MO{MatchClause} & (\vf\:\tp = \ve) & (\vf\:\tts) &
443+% & \MO{MatchClause} & (\vf\:\tp = \ve) & (\vf\:\tts) &
439− \MoRet{
444+% \MoRet{
440− \AR{
445+% \AR{
441− \Success\:\ve[\tx/\tv]\hg\mbox{(if $\vec{\MO{MatchArg}}\:\tp\:\tts \mq \Success\:(\tx,\tv)$)}\\
446+% \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$)}\\
447+% \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$)}\\
448+% \Failure\;\;\;\;\;\hg\hg\mbox{(if $\vec{\MO{MatchArg}}\:\tp\:\tts \mq \Failure$)}\\
444− }
449+% }
445− } \\
450+% } \\
446−& \MO{MatchClause} & (\vf\:\tp = \ve) & \cdot & \MoRet{\Failure} \\
451+% & \MO{MatchClause} & (\vf\:\tp = \ve) & \cdot & \MoRet{\Failure} \\
447−}
452+% }
448−\medskip\\
453+% \medskip\\
449−\PA{\A\A}{
454+% \PA{\A\A}{
450−& \MO{Match} & (\vc ; \tc) & \ve &
455+% & \MO{Match} & (\vc ; \tc) & \ve &
451− \MoRet{
456+% \MoRet{
452− \AR{
457+% \AR{
453− \Success\;\vx\;\;\hg\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Success\:\vx)$} \\
458+% \Success\;\vx\;\;\hg\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Success\:\vx)$} \\
454− \Blocked\hg\;\;\;\;\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Blocked)$} \\
459+% \Blocked\hg\;\;\;\;\mbox{(if $\MO{MatchClause}\:\vc\:\ve \mq \Blocked)$} \\
455− \MO{Match}\:\tc\:\ve\hg\mbox{(otherwise)}
460+% \MO{Match}\:\tc\:\ve\hg\mbox{(otherwise)}
456− }
461+% }
457− } \\
462+% } \\
458−& \MO{Match} & \cdot & \ve & \MoRet{\Failure}
463+% & \MO{Match} & \cdot & \ve & \MoRet{\Failure}
459−}
464+% }
460−}
465+% }
461−}
466+% }
462−{Pattern matching semantics}
467+% {Pattern matching semantics}
463−{pmatch}
468+% {pmatch}
464−
469+%
465−By convention, we write the application of a meta-operation $\MO{Op}$
470+% By convention, we write the application of a meta-operation $\MO{Op}$
466−across a vector $\tx$ as $\vec{\MO{Op}}\:\tx$.
471+% across a vector $\tx$ as $\vec{\MO{Op}}\:\tx$.
467−In practice, for efficiency, pattern matching is implemented by compiling the match clauses to
472+% In practice, for efficiency, pattern matching is implemented by compiling the match clauses to
468−a tree of case expressions~\cite{Augustsson1985}.
473+% a tree of case expressions~\cite{Augustsson1985}.
469
474
470\subsection{Totality checking}
475\subsection{Totality checking}
471
476
472−In order to ensure termination of type checking
477+In order to ensure termination of type checking we must distinguish terms for
473−we must distinguish terms for which evaluation definitely terminates, and
478+which evaluation definitely terminates, and those which may diverge. \TT{}
474−those which may diverge. \TT{} takes a simple but pragmatic and
475−effective approach to termination
479+takes a simple but pragmatic and effective approach to termination checking:
476−checking: any functions which do not satisfy a syntactic constraint on recursive
480+any functions which do not satisfy a syntactic constraint on recursive calls
477−calls are marked as \emph{partial}. Additionally, any function which calls a partial
481+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
482function 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
483partial. The syntactic constraint we use is that a recursive call in a pattern
480−clause must have an argument which is structurally smaller than an input argument
484+clause must have an argument which is structurally smaller than an input
481−in the same position. \TT{} also marks functions which do not cover all possible
485+argument in the same position. \TT{} also marks functions which do not cover
482−inputs as partial. This totality checking is independent of the rest of the type
486+all possible inputs as partial. This totality checking is independent of the
483−theory, and can be extended.
487+rest of the type theory, and can be extended.
484
488
485−This approach, separating the termination requirement from the type theory, means that
489+This approach, separating the termination requirement from the type theory,
486−an \Idris{} programmer makes the decision about the importance of totality for each
490+means that an \Idris{} programmer makes the decision about the importance of
487−function rather than having the totality requirement imposed by the type theory.
491+totality for each function rather than having the totality requirement imposed
492+by the type theory.
488
493
489\subsection{From \Idris{} to \TT{}}
494\subsection{From \Idris{} to \TT{}}
490
495
@@ -574,7 +579,7 @@ switching to \TT{} notation from the ASCII \Idris{} syntax.
574{Pairwise addition of vectors in \TT{}}
579{Pairwise addition of vectors in \TT{}}
575{ttvadd}
580{ttvadd}
576
581
577−\subsubsection{An observation}
582+\subsubsection{An Observation: Programming vs Theorem Proving}
578
583
579\Idris{} programs may contain several high level constructs not present in \TT{}, such
584\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}
585as implicit arguments, type classes, \texttt{where} clauses, pattern matching \texttt{let}