on master help sign in
Error handling interpreter needs Functor instance (via bishboria)
commit3c6fe065d32c38a789640d816d311decf062306a authorEdwin Brady <ecb10@st-andrews.ac.uk> dateFri 8 Feb 2013, 10:25 (14 years ago) parentf868390
1 file changed+16−9
tutorial/classes.tex+16−9
tutorial/classes.tex+16−9
@@ -355,9 +355,10 @@ m_add' x y = m_app (m_app (Just (+)) x) y
355355\useverb{maddb}
356356
357357\noindent
358−Rather than having to insert \texttt{m\_app} everywhere there is an application, we can
359−use \remph{idiom brackets} to do the job for us. To do this, we use the \texttt{Applicative}
360−class, which captures the notion of application for a data type:
358+Rather than having to insert \texttt{m\_app} everywhere there is an
359+application, we can use \remph{idiom brackets} to do the job for us. To do
360+this, we use the \texttt{Applicative} class, which captures the notion of
361+application for a data type:
361362
362363\begin{SaveVerbatim}{applicative}
363364
@@ -371,8 +372,8 @@ class Applicative (f : Type -> Type) where
371372\useverb{applicative}
372373
373374\noindent
374−\texttt{Maybe} is made an instance of \texttt{Applicative} as follows, where \texttt{<\$>}
375−is defined in the same way as \texttt{m\_app} above:
375+\texttt{Maybe} is made an instance of \texttt{Applicative} as follows, where
376+\texttt{<\$>} is defined in the same way as \texttt{m\_app} above:
376377
377378\begin{SaveVerbatim}{maybeapp}
378379
@@ -443,15 +444,21 @@ fetch x = MkEval (\e => fetchVal e) where
443444\useverb{fetch}
444445
445446\noindent
446−When defining an evaluator for the language, we will be applying functions in the
447−context of an \texttt{Eval}, so it is natural to make \texttt{Eval} an instance
448−of \texttt{Applicative}:
447+When defining an evaluator for the language, we will be applying functions in
448+the context of an \texttt{Eval}, so it is natural to make \texttt{Eval} an
449+instance of \texttt{Applicative}.
450+Before \texttt{Eval} can be an instance of
451+\texttt{Applicative} it is necessary to make \texttt{Eval} an instance of
452+\texttt{Functor}:
449453
450454\begin{SaveVerbatim}{}
451455
456+instance Functor Eval where
457+ fmap f (MkEval g) = MkEval (\e => fmap f (g e))
458+
452459instance Applicative Eval where
453460 pure x = MkEval (\e => Just x)
454−
461+
455462 (<$>) (MkEval f) (MkEval g) = MkEval (\x => app (f x) (g x)) where
456463 app : Maybe (a -> b) -> Maybe a -> Maybe b
457464 app (Just fx) (Just gx) = Just (fx gx)