@@ -190,6 +190,8 @@ sortAndShow xs = show (sort xs);
191\subsection{Monads and \texttt{do}-notation}
191\subsection{Monads and \texttt{do}-notation}
193So far, we have seen single parameter type classes, where the parameter is of type
195So far, we have seen single parameter type classes, where the parameter is of type
194\texttt{Set}. In general, there can be any number (greater than 0) of parameters,
196\texttt{Set}. In general, there can be any number (greater than 0) of parameters,
195and the parameters can have \remph{any} type.
197and the parameters can have \remph{any} type.
@@ -263,3 +265,54 @@ Nothing : Maybe Int
264\end{SaveVerbatim}
266\end{SaveVerbatim}
265\useverb{maybeaddt}
267\useverb{maybeaddt}
269+\subsubsection*{Monad comprehensions}
271+The list comprehension notation we saw in Section \ref{sec:listcomp} is more
272+general, and applies to anything which is an instance of \texttt{MonadPlus}:
274+\begin{SaveVerbatim}{monadplus}
276+class Monad m => MonadPlus (m : Set -> Set) where {
277+ mplus : m a -> m a -> m a;
285+In general, a comprehension takes the form \texttt{[ exp | qual ]} where
286+\texttt{qual} can be one of:
289+\item A generator \texttt{x <- e}
290+\item A \emph{guard}, which is an expression of type \texttt{Bool}
291+\item A let binding \texttt{let x = e}
295+To translate a comprehension \texttt{[exp | qual1, qual2, ..., qualn]}, first
297+\texttt{qual} which is a \emph{guard} is translated to \texttt{guard qual}, using
298+the following function:
300+\begin{SaveVerbatim}{guardq}
302+guard : MonadPlus m => Bool -> m ()
308+Then the comprehension is converted to \texttt{do} notation:
310+\begin{SaveVerbatim}{comptrans}
312+do { qual1; qual2; ...; qualn; return exp; }