summaryrefslogtreecommitdiff
path: root/compiler/utils/Maybes.lhs
diff options
context:
space:
mode:
authorTwan van Laarhoven <twanvl@gmail.com>2008-01-17 20:20:51 +0000
committerTwan van Laarhoven <twanvl@gmail.com>2008-01-17 20:20:51 +0000
commitbc845b714132a897032502536fea8cd018ce325b (patch)
treef69a74c0939b7077942367a9022f58dbd3e3e695 /compiler/utils/Maybes.lhs
parentda6f25437e59e97791a1fea7ce2f9ce4dccc57f6 (diff)
downloadhaskell-bc845b714132a897032502536fea8cd018ce325b.tar.gz
Added MaybeT monad transformer to utils/Maybes
Diffstat (limited to 'compiler/utils/Maybes.lhs')
-rw-r--r--compiler/utils/Maybes.lhs21
1 files changed, 21 insertions, 0 deletions
diff --git a/compiler/utils/Maybes.lhs b/compiler/utils/Maybes.lhs
index 1d43365dce..d4f8346f14 100644
--- a/compiler/utils/Maybes.lhs
+++ b/compiler/utils/Maybes.lhs
@@ -17,6 +17,7 @@ module Maybes (
expectJust,
maybeToBool,
+ MaybeT(..)
) where
import Data.Maybe
@@ -82,6 +83,26 @@ orElse :: Maybe a -> a -> a
Nothing `orElse` y = y
\end{code}
+%************************************************************************
+%* *
+\subsection[MaybeT type]{The @MaybeT@ monad transformer}
+%* *
+%************************************************************************
+
+\begin{code}
+
+newtype MaybeT m a = MaybeT {runMaybeT :: m (Maybe a)}
+
+instance Functor m => Functor (MaybeT m) where
+ fmap f x = MaybeT $ fmap (fmap f) $ runMaybeT x
+
+instance Monad m => Monad (MaybeT m) where
+ return = MaybeT . return . Just
+ x >>= f = MaybeT $ runMaybeT x >>= maybe (return Nothing) (runMaybeT . f)
+ fail _ = MaybeT $ return Nothing
+
+\end{code}
+
%************************************************************************
%* *