@@ -22,7 +22,7 @@ one or more \TT{} declarations.
23\subsection{The Development Calculus \TTdev}
23\subsection{The Development Calculus \TTdev}
25−We build \TT{} expressions by using high level \Idris{} expressions to
25+\TT{} expressions are built by using high level \Idris{} expressions to
26direct a tactic based theorem prover, which builds the \TT{} expressions
26direct a tactic based theorem prover, which builds the \TT{} expressions
27step by step, by refinement. In order to build expressions in this way,
27step by step, by refinement. In order to build expressions in this way,
28the type theory needs to support
28the type theory needs to support
@@ -129,7 +129,7 @@ the final proof term, which must be a \TT{} program (i.e. does not contain any o
129\demph{pure}. Although a pure term does not contain hole or guess bindings, it may
129\demph{pure}. Although a pure term does not contain hole or guess bindings, it may
130nevertheless \remph{refer} to hole- or guess-bound variables.
130nevertheless \remph{refer} to hole- or guess-bound variables.
132−We initialise a proof state with the $\MO{NewProof}$ operation. Given a global
132+The proof state is initialised with the $\MO{NewProof}$ operation. Given a global
133context $\vC$, $\MO{NewProof}\:\vt$ sets up the proof state as:
133context $\vC$, $\MO{NewProof}\:\vt$ sets up the proof state as:
@@ -137,7 +137,7 @@ context $\vC$, $\MO{NewProof}\:\vt$ sets up the proof state as:
139The local context is initially empty, and the initial hole queue is the $\vx$ standing for
139The local context is initially empty, and the initial hole queue is the $\vx$ standing for
140−the entire expression. We can reset the proof term with the $\MO{NewTerm}$ operation.
140+the entire expression. The proof term is reset with the $\MO{NewTerm}$ operation.
141In an existing proof state $(\vC, \Delta, \ve, \vQ)$,
141In an existing proof state $(\vC, \Delta, \ve, \vQ)$,
142$\MO{NewTerm}\:\vt$ discards the proof term and hole queue, and
142$\MO{NewTerm}\:\vt$ discards the proof term and hole queue, and
143updates the proof state to:
143updates the proof state to:
@@ -147,8 +147,8 @@ updates the proof state to:
150−This allows us in particular to use pattern bindings from the left hand side of a pattern
150+This allows the elaborator to use pattern bindings from the left
151−matching definition in the term on the right hand side.
151+hand side of a pattern matching definition in the term on the right hand side.
153\subsection{System State}
153\subsection{System State}
@@ -193,7 +193,7 @@ vAdd : (a : _) -> (n : _) -> (c : Num a) ->
193\useverb{vAddImpT}
193\useverb{vAddImpT}
196−For \tFN{vAdd}, we record that \texttt{a} and \texttt{n} are implicit,
196+For \tFN{vAdd}, the state records that \texttt{a} and \texttt{n} are implicit,
197\texttt{c} is a constraint, and \texttt{xs} and \texttt{ys} are explicit. When
197\texttt{c} is a constraint, and \texttt{xs} and \texttt{ys} are explicit. When
198the elaborator encounters an application of \tFN{vAdd}, it knows that unless these arguments
198the elaborator encounters an application of \tFN{vAdd}, it knows that unless these arguments
199are given explicitly, the application must be expanded.
199are given explicitly, the application must be expanded.
@@ -264,7 +264,7 @@ keeping $\vx$ at the head:
266An obvious difficulty is in ensuring that names are unique throughout a proof term.
266An obvious difficulty is in ensuring that names are unique throughout a proof term.
267−In the implementation, we ensure that any hole created by the $\MO{Claim}$ tactic
267+The implementation ensures that any hole created by the $\MO{Claim}$ tactic
268has a unique name by checking against existing names in scope and modifying
268has a unique name by checking against existing names in scope and modifying
269the name if necessary. In this paper, we will assume that all created names are fresh.
269the name if necessary. In this paper, we will assume that all created names are fresh.
@@ -296,13 +296,14 @@ For example, consider the following proof term:
299−If $\vx$ is in focus (i.e., at the head of the hole queue) and we attempt to
299+If $\vx$ is in focus (i.e., at the head of the hole queue) and the elaborator
300$\MO{Fill}$ it with an $\TC{Int}$ value $42$, we have:
301$\MO{Fill}$ it with an $\TC{Int}$ value $42$, we have:
302\begin{itemize}
303\begin{itemize}
303\item $\Check\:42\:\mq\:\TC{Int}$
304\item $\Check\:42\:\mq\:\TC{Int}$
304\item Unifying $\TC{Int}$ with $\vA$ (the type of $\vx$) is only possible if
305\item Unifying $\TC{Int}$ with $\vA$ (the type of $\vx$) is only possible if
305−$\vA\:=\:\TC{Int}$, so we solve $\vA$.
306+$\vA\:=\:\TC{Int}$, so solve $\vA$.
306\end{itemize}
307\end{itemize}
@@ -328,6 +329,10 @@ the guess is substituted into its scope:
332+The two step process, with $\MO{Fill}$ followed by $\MO{Solve}$, allows the elaborator
333+to work safely with incomplete terms, since an incomplete guess binding has no
334+computational force. Once a term is complete in a guess binding, it may be substituted into the
335+scope of the binding safely.
331In each of these tactics, if any step fails, or the term in focus does not take
336In each of these tactics, if any step fails, or the term in focus does not take
332the correct form (e.g. is not a guess in the case of $\MO{Solve}$ or not a hole
337the correct form (e.g. is not a guess in the case of $\MO{Solve}$ or not a hole
333in the case of $\MO{Claim}$ and $\MO{Fill}$, the entire tactic fails. We can
338in the case of $\MO{Claim}$ and $\MO{Fill}$, the entire tactic fails. We can
@@ -347,8 +352,8 @@ problem) and is handled by $\MO{Try}$.
348\subsubsection{Creating binders}
353\subsubsection{Creating binders}
350−We also define primitive tactics for constructing binders. We can create a $\lambda$
355+We also define primitive tactics for constructing binders. Creating a $\lambda$
351−binding if the goal normalises to a function type:
356+binding requires that the goal normalises to a function type:
354\PA{\A\A}{
359\PA{\A\A}{
@@ -362,7 +367,7 @@ binding if the goal normalises to a function type:
365−We can create a $\forall$ binding if the goal is a $\TC{Set}$:
370+Creating a $\forall$ binding requires that the goal is a $\TC{Set}$:
368\PA{\A\A}{
373\PA{\A\A}{
@@ -376,7 +381,7 @@ We can create a $\forall$ binding if the goal is a $\TC{Set}$:
379−To create a $\LET$ binding, we give a type and a value.
384+Creating a $\LET$ binding requires a type and a value.
382\PA{\A\A}{
387\PA{\A\A}{
@@ -392,8 +397,8 @@ To create a $\LET$ binding, we give a type and a value.
394Each of these tactics require the term in focus to be of the form $\hole{\vx}{\vT}\SC\vx$.
399Each of these tactics require the term in focus to be of the form $\hole{\vx}{\vT}\SC\vx$.
395−This is important, because if the scope of the binding was an arbitrary expression $\ve$,
400+This is important, because if the scope of the binding were an arbitrary expression $\ve$,
396−the binder would be scoped across this whole expression rather than the subexpression
401+the binder would be scoped across this \emph{whole} expression rather than the subexpression
397$\vx$ as intended.
402$\vx$ as intended.
398The $\MO{attack}$ tactic ensures that a hole
403The $\MO{attack}$ tactic ensures that a hole
399is in the appropriate form, creating a new hole $\vh$ which is placed at the head
404is in the appropriate form, creating a new hole $\vh$ which is placed at the head
@@ -779,7 +784,7 @@ There are two cases to consider when elaborating applications:
779\item Applying an expression, which does not have implicit arguments, to an argument.
784\item Applying an expression, which does not have implicit arguments, to an argument.
780\end{itemize}
785\end{itemize}
782−In the first case, we must expand the application to include implicit arguments.
787+In the first case, the elaborator must expand the application to include implicit arguments.
783For example \texttt{vAdd xs ys} is expanded to
788For example \texttt{vAdd xs ys} is expanded to
784\texttt{vAdd \{a=\_\} \{n=\_\} \{\{\_\}\} xs ys}, adding the implicit arguments
789\texttt{vAdd \{a=\_\} \{n=\_\} \{\{\_\}\} xs ys}, adding the implicit arguments
785\texttt{a} and \texttt{n} and a type class instance argument. The meta-operation
790\texttt{a} and \texttt{n} and a type class instance argument. The meta-operation
@@ -825,9 +830,8 @@ arguments first.
825Elaborating a global function application makes a $\MO{Claim}$ for each argument
830Elaborating a global function application makes a $\MO{Claim}$ for each argument
826in turn. Then the function is elaborated, applied to each of the claimed arguments.
831in turn. Then the function is elaborated, applied to each of the claimed arguments.
827Finally, each argument which has been given explicitly is elaborated. If this fails,
832Finally, each argument which has been given explicitly is elaborated. If this fails,
828−elaboration tries again in the opposite order. Finally, we resolve any type
833+elaboration tries again in the opposite order. Finally, any type
829−class instance arguments with the built-in $\MO{Instance}$ tactic (see Section
834+class instance arguments are resolved with the built-in $\MO{Instance}$ tactic.
830−\ref{sect:instance}).
833\ttinterp{\vx\:\ta}\:\mq\:
837\ttinterp{\vx\:\ta}\:\mq\:
@@ -860,9 +864,9 @@ in detail in Section \ref{sect:instance}.
861To elaborate a simple function application, of an arbitrary expression to an arbitrary
865To elaborate a simple function application, of an arbitrary expression to an arbitrary
862argument, we need not worry about implicit arguments or class constraints. Instead,
866argument, we need not worry about implicit arguments or class constraints. Instead,
863−we simply elaborate the function and argument, and apply the results. Since elaboration
867+the function and argument are elaborated, and the results applied. Since elaboration
864−is type directed, however, we do need to construct an appropriate type for the function.
868+is type directed, however, there must be an appropriate type for the function.
865−As before, we try elaboration in two orders, as we may learn something from elaborating
869+As before, elaboration tries two orders, as it may learn something from elaborating
866the function or argument which helps with unification:
870the function or argument which helps with unification:
@@ -897,8 +901,8 @@ current proof state. $\MO{Elab}$ uses $\ttinterp{\cdot}$ to help translate
898\subsubsection{Elaborating Type Declarations}
902\subsubsection{Elaborating Type Declarations}
900−To elaborate a type declaration, we simply create a new proof state,
904+Elaborating a type declaration involves creating a new proof state,
901−translate the \IdrisM{} type to a \TT{} type, then add the resulting type
905+translating the \IdrisM{} type to a \TT{} type, then adding the resulting type
902as a \TT{} declaration:
906as a \TT{} declaration:
@@ -916,10 +920,10 @@ into the context.
917\subsubsection{Elaborating Data Types}
921\subsubsection{Elaborating Data Types}
919−To elaborate a data declaration, we need to elaborate the type declaration itself,
923+Elaborating a data declaration involves elaborating the type declaration itself,
920as a normal type declaration. This ensures that the type is in scope when
924as a normal type declaration. This ensures that the type is in scope when
921−elaborating the constructor types. We then use the results to build a \TT{} data
925+elaborating the constructor types. Then using the results, the elaborator
922−declaration.
926+adds a \TT{} data declaration.
@@ -942,7 +946,7 @@ declaration.
943\subsubsection{Elaborating Pattern Matching}
947\subsubsection{Elaborating Pattern Matching}
945−To elaborate a pattern matching definition, we work clause by clause, elaborating
949+Elaborating a pattern matching definition works clause by clause, elaborating
946the left and right hand sides in turn. $\MO{ElabClause}$ returns the elaborated
950the left and right hand sides in turn. $\MO{ElabClause}$ returns the elaborated
947left and right hand sides, and may have the side effect of adding entries
951left and right hand sides, and may have the side effect of adding entries
948to the global context, such as definitions in $\iwhere$ clauses.
952to the global context, such as definitions in $\iwhere$ clauses.
@@ -952,17 +956,17 @@ First, let us consider the simplest case, with no $\iwhere$ clause:
952\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve)\:\mq\:?
956\MO{ElabClause}\:(\vx\:\ttt\:=\:\ve)\:\mq\:?
955−How do we elaborate the left hand side, given that elaboration is type directed, and
959+How does the left hand side get elaborated, given that elaboration is type directed, and
956−we do not know its type until after we have elaborated it?
960+its type is not known until after elaboration?
957−We can achieve this without any change to the elaborator by defining
961+This can be achieved without any change to the elaborator by defining
958a type $\TC{Infer}$:
962a type $\TC{Infer}$:
961\Data\hg\TC{Infer}\Hab\Set\hg\Where\hg\DC{MkInfer}\Hab\all{\va}{\Set}\SC\va\to\TC{Infer}
965\Data\hg\TC{Infer}\Hab\Set\hg\Where\hg\DC{MkInfer}\Hab\all{\va}{\Set}\SC\va\to\TC{Infer}
964−Now to elaborate the left hand side, we elaborate $\DC{MkInfer}\:\_\:(\vx\:\ttt)$ and
968+Now elaboration of the left hand side proceeds by elaborating $\DC{MkInfer}\:\_\:(\vx\:\ttt)$ and
965−extract the value and unified type when complete.
969+extracting the value and unified type when complete.
@@ -984,8 +988,8 @@ This infers a type for the left hand side, creating pattern variable bindings. T
984it elaborates the right hand side using the inferred type of the left hand side
988it elaborates the right hand side using the inferred type of the left hand side
985and the inferred pattern bindings, which are retrieved from the state with the
989and the inferred pattern bindings, which are retrieved from the state with the
986$\MO{Patterns}$ operation. Finally, it returns a pattern clause in \TT{} form.
990$\MO{Patterns}$ operation. Finally, it returns a pattern clause in \TT{} form.
987−To elaborate a collection of pattern clauses, we simply map $\MO{ElabClause}$ over
991+Elaborating a collection of pattern clauses then proceeds by mapping $\MO{ElabClause}$ over
988−the clauses and add the resulting collection to \TT{}.
992+the clauses and adding the resulting collection to \TT{}.
@@ -998,9 +1002,9 @@ the clauses and add the resulting collection to \TT{}.
1000Elaborating a clause is made only slightly more complex by the presence of
1004Elaborating a clause is made only slightly more complex by the presence of
1001−$\iwhere$ clauses. In this case, we elaborate the left hand side, add the pattern
1005+$\iwhere$ clauses. In this case, after elaborating the left hand side, the pattern
1002−bound variables as extra arguments to the declarations in the $\iwhere$ block,
1006+bound variables are added as extra arguments to the declarations in the $\iwhere$ block,
1003−and recursively elaborate before elaborating the right hand side.
1007+which are then recursively elaborated before the right hand side.
@@ -1028,7 +1032,7 @@ the right hand side $\ve$ to use the lifted definitions, rather than the origina
1028\subsubsection{Elaborating the \texttt{with} rule}
1032\subsubsection{Elaborating the \texttt{with} rule}
1030The \texttt{with} rule allows \remph{dependent} pattern matching on intermediate values.
1034The \texttt{with} rule allows \remph{dependent} pattern matching on intermediate values.
1031−To translate this into \TT{}, we need to construct an auxiliary top level
1035+Translating this into \TT{} involves constructing an auxiliary top level
1032definition for
1036definition for
1033the intermediate pattern match. For example, recall \tFN{natToBin}:
1037the intermediate pattern match. For example, recall \tFN{natToBin}:
@@ -1055,8 +1059,7 @@ natToBin' (S (j + j)) odd = True :: natToBin j
1056\useverb{natToBinwp}
1060\useverb{natToBinwp}
1058−The difference when using the \texttt{with} rule is that there is no need for
1062+\noindent
1059−the explicit type declaration for the auxiliary function \texttt{natToBin}.
1060Elaboration of \texttt{with} builds the type declaration and auxiliary function
1063Elaboration of \texttt{with} builds the type declaration and auxiliary function
1061automatically:
1064automatically:
@@ -1115,8 +1118,8 @@ a type class declaration elaborates to a record type containing all of the funct
1115in the class, and an instance is simply an instance of the record. The methods
1118in the class, and an instance is simply an instance of the record. The methods
1116are translated to top level functions which extract the relevant function from the
1119are translated to top level functions which extract the relevant function from the
1117dictionary.
1120dictionary.
1118−For example, for the \texttt{Show} type class, we translate the \texttt{class}
1121+For example, for the \texttt{Show} type class, the \texttt{class}
1119−declaration to:
1122+declaration is translated to:
1121\begin{SaveVerbatim}{showdata}
1124\begin{SaveVerbatim}{showdata}
@@ -1145,7 +1148,7 @@ showNat = instanceShow show' where
1146The call to \tFN{show} in the body of the instance uses the top level \tFN{show}
1149The call to \tFN{show} in the body of the instance uses the top level \tFN{show}
1147function, with the type class instance resolved by the elaborator.
1150function, with the type class instance resolved by the elaborator.
1148−Where instances have constraints, we can simply pass these constraints on to
1151+Where instances have constraints, these constraints are passed on to
1149the function declaration for the instance. For example, for \texttt{Show (List a)}:
1152the function declaration for the instance. For example, for \texttt{Show (List a)}:
1151\begin{SaveVerbatim}{showlista}
1154\begin{SaveVerbatim}{showlista}
@@ -1160,9 +1163,9 @@ showList = instanceShow show' where
1160\useverb{showlista}
1163\useverb{showlista}
1162\noindent
1165\noindent
1163−Where classes have constraints, we can again pass the
1166+Where classes have constraints, the
1164−constraints on to the data type declaration, and leave resolving the
1167+constraints are again passed on to the data type declaration, and resolved by the
1165−constraints to the elaborator. For example, for \texttt{Ord}:
1168+elaborator. For example, for \texttt{Ord}:
1167\begin{SaveVerbatim}{eqorda}
1170\begin{SaveVerbatim}{eqorda}
@@ -1183,6 +1186,9 @@ data Ord : Set -> Set where
1183 instanceOrd : Eq a => (compare : a -> a -> Ordering) ->
1186 instanceOrd : Eq a => (compare : a -> a -> Ordering) ->
1184 (max : a -> a -> a) -> Ord a
1187 (max : a -> a -> a) -> Ord a
1191+\begin{SaveVerbatim}{orddatab}
1186compare : Ord a -> a -> a -> Ordering
1192compare : Ord a -> a -> a -> Ordering
1187compare (instanceOrd compare' max') = compare'
1193compare (instanceOrd compare' max') = compare'
@@ -1192,8 +1198,11 @@ max (instanceOrd compare' max') = max'
1192\end{SaveVerbatim}
1198\end{SaveVerbatim}
1193\useverb{orddata}
1199\useverb{orddata}
1195−We also add functions to retrieve parent classes from a dictionary, to assist
1204+The elaborator also adds functions to retrieve parent classes from a dictionary, to assist
1196−with type class resolution. In this case, we add:
1205+with type class resolution. In this case:
1198\begin{SaveVerbatim}{ordparent}
1207\begin{SaveVerbatim}{ordparent}
@@ -1204,7 +1213,7 @@ OrdEq (instanceOrd {{eq}} compare' max') = eq
1204\useverb{ordparent}
1213\useverb{ordparent}
1206\noindent
1215\noindent
1207−In general, we elaborate \texttt{class} declarations as follows:
1216+In general, \texttt{class} declarations are elaborated as follows:
@@ -1242,7 +1251,7 @@ In general, we elaborate \texttt{class} declarations as follows:
1244\noindent
1253\noindent
1245−Then we elaborate \texttt{instance} declarations as follows:
1254+Then \texttt{instance} declarations are elaborated as follows:
@@ -1275,7 +1284,7 @@ constructing programs in \TT{}.
1276\label{sect:instance}
1285\label{sect:instance}
1278−When elaborating applications, recall that we fill in implicit arguments by
1287+When elaborating applications, recall that implicit arguments are filled in by
1279unification, and type class constraints by an $\MO{Instance}$ tactic which searches
1288unification, and type class constraints by an $\MO{Instance}$ tactic which searches
1280the context for an instance of the required type class.
1289the context for an instance of the required type class.
@@ -1298,9 +1307,9 @@ basic primitive for proof search, and is defined as follows:
1301−For example, in the following function, we resolve the type class constraint required
1310+For example, in the following function, the type class constraint required for
1302−for the $\texttt{>}$ operator by finding an instance for \texttt{Ord a} in the local
1311+the $\texttt{>}$ operator is resolved by finding an instance for \texttt{Ord a}
1303−context:
1312+in the local context:
1305\begin{SaveVerbatim}{ordlocal}
1314\begin{SaveVerbatim}{ordlocal}
@@ -1344,11 +1353,11 @@ we constrain the search by recording which instances are defined for each type c
1344and by ensuring that a recursive call of $\MO{Instance}$ is either searching for
1353and by ensuring that a recursive call of $\MO{Instance}$ is either searching for
1345a different class, or a structurally smaller instance of the current class.
1354a different class, or a structurally smaller instance of the current class.
1347−For example, in the following function, we resolve the constraints required by the
1356+For example, in the following function, the constraints required by the
1348−\tFN{show} function by a global search which locates a \tTC{Show} instance for
1357+\tFN{show} function are resolved by a global search which locates a \tTC{Show}
1349−\tTC{List a}, which in turn requires a \tTC{Show} instance for \texttt{a}.
1358+instance for \tTC{List a}, which in turn requires a \tTC{Show} instance for
1350−In this case \texttt{a} is instantiated by \tTC{Int}, so we finish by locating
1359+\texttt{a}. In this case \texttt{a} is instantiated by \tTC{Int}, so resolution finishes
1351−a \tTC{Show} instance for \tTC{Int}:
1360+by locating a \tTC{Show} instance for \tTC{Int}:
1353\begin{SaveVerbatim}{showinst}
1362\begin{SaveVerbatim}{showinst}
@@ -1428,8 +1437,8 @@ append_rec a m n x xs ys = app xs ys
1428\end{SaveVerbatim}
1437\end{SaveVerbatim}
1429\useverb{appendrec_def}
1438\useverb{appendrec_def}
1431−To elaborate a metavariable, we simply add a new top level definition, and apply
1440+Elaborating a metavariable involves adding a new top level definition which is applied
1432−it to the current context:
1441+to all of the variables in local scope.
1435\ttinterp{\mvar{\vx}}\:\mq\:
1444\ttinterp{\mvar{\vx}}\:\mq\:
@@ -1445,8 +1454,9 @@ it to the current context:
1446A \texttt{case} expression allows pattern matching on intermediate values. The difficulty
1455A \texttt{case} expression allows pattern matching on intermediate values. The difficulty
1447in elaborating \texttt{case} expressions is that \TT{} allows matching only on
1456in elaborating \texttt{case} expressions is that \TT{} allows matching only on
1448−\emph{top level} values. To elaborate a \texttt{case} expression, therefore,
1457+\emph{top level} values. Elaborating a \texttt{case} expression, therefore,
1449−we create a new top level function standing for the expression, and apply it. The natural
1458+involves creating a new top level function standing for the expression, and applying it.
1450way to implement this is to use a metavariable. For example, we have already seen
1460way to implement this is to use a metavariable. For example, we have already seen
1451\texttt{lookup\_default}:
1461\texttt{lookup\_default}:
@@ -1467,9 +1477,9 @@ lookup_default_case i xs def (Just x) = x
1467\end{SaveVerbatim}
1477\end{SaveVerbatim}
1468\useverb{listlookupmv}
1478\useverb{listlookupmv}
1470−To elaborate a $\icase$ expression, we begin by \texttt{let} binding the scrutinee of
1480+To elaborate a $\icase$ expression involves \texttt{let} binding the scrutinee of
1471the $\icase$ expression and creating a metavariable $\VV{xcase}$ to implement the
1481the $\icase$ expression and creating a metavariable $\VV{xcase}$ to implement the
1472−pattern matching. We then build a new pattern matching definition for $\VV{xcase}$
1482+pattern matching. Then the elaborator builds a new pattern matching definition for $\VV{xcase}$
1473and elaborate it:
1483and elaborate it: