@@ -530,9 +530,9 @@ a Coq proof script.
530\newcommand{\iof}{\mathtt{of}}
530\newcommand{\iof}{\mathtt{of}}
531\newcommand{\ilet}[2]{\mathtt{let}\;#1\;\mathtt{=}\;#2\;\mathtt{in}}
531\newcommand{\ilet}[2]{\mathtt{let}\;#1\;\mathtt{=}\;#2\;\mathtt{in}}
532\newcommand{\ilam}[1]{\mathtt{\backslash}\;#1\;\mathtt{=>}}
532\newcommand{\ilam}[1]{\mathtt{\backslash}\;#1\;\mathtt{=>}}
533−\newcommand{\iarg}[2]{\mbox{\texttt{\{ $#1$ = $#2$ \}}}}
533+\newcommand{\iarg}[2]{\mbox{\texttt{\{$#1$ = $#2$\}}}}
534\newcommand{\ihab}[2]{\mbox{\texttt{$#1$ : $#2$}}}
534\newcommand{\ihab}[2]{\mbox{\texttt{$#1$ : $#2$}}}
535−\newcommand{\carg}[1]{\mbox{\texttt{\{\{ $#1$ \}\}}}}
535+\newcommand{\carg}[1]{\mbox{\texttt{\{\{$#1$\}\}}}}
536\newcommand{\fatarrow}{\mbox{\texttt{=>}}}
536\newcommand{\fatarrow}{\mbox{\texttt{=>}}}
537\newcommand{\ibar}{\mbox{\texttt{|}}}
537\newcommand{\ibar}{\mbox{\texttt{|}}}
538\newcommand{\mvar}[1]{\mbox{\texttt{?}}#1}
538\newcommand{\mvar}[1]{\mbox{\texttt{?}}#1}
@@ -600,7 +600,7 @@ a Coq proof script.
600\medskip\\
600\medskip\\
601\VV{cdecl} ::= & \iclass\;[\VV{constr}]\;\vx\;(\ihab{\tx}{\ttt})\;\iwhere\;\td
601\VV{cdecl} ::= & \iclass\;[\VV{constr}]\;\vx\;(\ihab{\tx}{\ttt})\;\iwhere\;\td
603−\VV{idecl} ::= & \iinstance\:[\VV{constr}]\;\vx\;\vt\;\iwhere\;\td
603+\VV{idecl} ::= & \iinstance\:[\VV{constr}]\;\vx\;\ttt\;\iwhere\;\td
604\end{array}
604\end{array}
605\end{array}
605\end{array}
@@ -789,7 +789,8 @@ Elaborating a global function application makes a $\MO{Claim}$ for each argument
789in turn. Then the function is elaborated, applied to each of the claimed arguments.
789in turn. Then the function is elaborated, applied to each of the claimed arguments.
790Finally, each argument which has been given explicitly is elaborated. If this fails,
790Finally, each argument which has been given explicitly is elaborated. If this fails,
791elaboration tries again in the opposite order. Finally, we resolve any type
791elaboration tries again in the opposite order. Finally, we resolve any type
792−class instance arguments with the built-in $\MO{Instance}$ tactic.
792+class instance arguments with the built-in $\MO{Instance}$ tactic (see Section
793+\ref{sect:instance}).
795\ttinterp{\vx\:\ta}\:\mq\:
796\ttinterp{\vx\:\ta}\:\mq\:
@@ -1069,6 +1070,163 @@ as $\tp$, which is verified by $\MO{CheckComplete}$.
1070\subsubsection{Elaborating Class and Instance Declarations}
1071\subsubsection{Elaborating Class and Instance Declarations}
1073+Type classes are implemented as dictionaries of functions for each instance. Therefore,
1074+a type class declaration elaborates to a record type containing all of the functions
1075+in the class, and an instance is simply an instance of the record. The methods
1076+are translated to top level functions which extract the relevant function from the
1078+For example, for the \texttt{Show} type class, we translate the \texttt{class}
1081+\begin{SaveVerbatim}{showdata}
1083+data Show : Set -> Set where
1084+ instanceShow : (show : a -> String) -> Show a
1086+show : Show a -> a -> String
1087+show (instanceShow show') = show'
1093+An instance for a type \texttt{a} is then a value of type \texttt{Show a}; for example for Nat:
1095+\begin{SaveVerbatim}{shownatf}
1098+showNat = instanceShow show' where
1099+ show' : Nat -> String
1101+ show' (S k) = "s" ++ show k
1106+The call to \tFN{show} in the body of the instance uses the top level \tFN{show}
1107+function, with the type class instance resolved by the elaborator.
1108+Where instances have constraints, we can simply pass these constraints on to
1109+the function declaration for the instance. For example, for \texttt{Show (List a)}:
1111+\begin{SaveVerbatim}{showlista}
1113+showList : Show a => Show (List a)
1114+showList = instanceShow show' where
1115+ show' : List a -> String
1117+ show' (x :: xs) = show x ++ " :: " ++ show xs
1123+Where classes have constraints, we can again pass the
1124+constraints on to the data type declaration, and leave resolving the
1125+constraints to the elaborator. For example, for \texttt{Ord}:
1127+\begin{SaveVerbatim}{eqorda}
1129+class Eq a => Ord a where
1130+ compare : a -> a -> Ordering
1132+ -- remaining methods elided
1140+\begin{SaveVerbatim}{orddata}
1142+data Ord : Set -> Set where
1143+ instanceOrd : Eq a => (compare : a -> a -> Ordering) ->
1144+ (max : a -> a -> a) -> Ord a
1146+compare : Ord a -> a -> a -> Ordering
1147+compare (instanceOrd compare' max') = compare'
1149+max : Ord a -> a -> a -> Ordering
1150+max (instanceOrd compare' max') = max'
1155+We also add functions to retrieve parent classes from a dictionary, to assist
1156+with type class resolution. In this case, we add:
1158+\begin{SaveVerbatim}{ordparent}
1160+OrdEq : Ord a -> Eq a
1161+OrdEq (instanceOrd {{eq}} compare' max') = eq
1166+In general, we elaborate \texttt{class} declarations as follows:
1170+\MO{Elab}\:(\iclass\;\piconst{\tc}\TC{C}\:(\ta\Hab\ttt)\:\iwhere\:\td)
1174+(\vec{\VV{meth}}\Hab\ttt)\gets\td\\
1176+\AR{(\RW{data}\:\TC{C}\Hab(\ta\Hab\ttt)\to\Set\:\iwhere\\
1177+\hg\DC{InstanceC}\Hab\piconst{\tc}\td\to\TC{C}\:\ta)}
1179+\vec{\MO{ElabMeth}}\:\vec{\VV{meth}}\:\td\\
1180+\vec{\MO{ElabParent}}\:\tc
1183+\MO{ElabMeth}\:(\vf\Hab\vt)\\
1184+\hg\hg\mq\:\RW{do}\:\AR{
1185+\MO{Elab}\:(\vf\Hab\TC{C}\:\ta\to\vt)\\
1186+\MO{Elab}\:(\vf\:(\DC{InstanceC}\:\vec{\VV{meth}})\:=\:\vec{\VV{meth}[\vf]})
1189+\MO{ElabParent}\:\vc\\
1190+\hg\hg\mq\:\RW{do}\:\AR{
1191+\MO{Elab}\:(\VV{cC}\Hab\TC{C}\:\ta\to\vc)\\
1192+\MO{Elab}\:(\VV{cC}\:(\DC{InstanceC}\:\carg{\vc'}\:\vec{\VV{meth}})\:=\:\vc')
1197+Then we elaborate \texttt{instance} declarations as follows:
1201+\MO{Elab}\:(\iinstance\;\piconst{\tc}\TC{C}\:\ttt\:\iwhere\:\td)\\
1202+\hg\hg\mq\:\RW{do}\:\AR{
1203+(\vec{\VV{meth}}, \vec{\VV{ty}})\gets\td\\
1204+\vec{\VV{ty'}}\:=\:\vec{\VV{ty}}[\ttt/\ta]\\
1205+\MO{Elab}\:(\FN{instanceC}\Hab\piconst{\tc}\TC{C}\:\ttt)\\
1207+(\FN{instanceC}\:=\:\DC{InstanceC}\;\vec{\VV{meth'}}\;\iwhere
1209+\hg\vec{\VV{meth'}}\Hab\vec{\VV{ty'}}\\
1216+Adding default definitions is straightforward: simply insert the default definition where
1217+there is a method missing in an \texttt{instance} declaration.
1219+Key point: we've added a higher level language construct simply by elaborating in terms of
1220+a lower level language construct. In particular, we don't need to worry about type
1221+class \remph{resolution}, because we have a tactic to do that.
1222+We can build a lot of extensions this way because we
1223+have effectively built, bottom up, an Embedded Domain Specific Language for constructing
1226+\subsubsection{The $\MO{Instance}$ tactic}
1228+\label{sect:instance}
1072\subsection{Syntax Extensions}
1230\subsection{Syntax Extensions}
1074\subsubsection{Metavariables}
1232\subsubsection{Metavariables}