X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FMaybes.lhs;h=d4f8346f1495cb003b6b89cc49abbe715e96c13c;hb=f5d4c3239e57b0396672ffc302961f84398d730e;hp=c977494d2b5f10d38d7e73504504470f0d1ad2c0;hpb=38ac36a32032a538db346411b119c47e4019c08f;p=ghc-hetmet.git diff --git a/compiler/utils/Maybes.lhs b/compiler/utils/Maybes.lhs index c977494..d4f8346 100644 --- a/compiler/utils/Maybes.lhs +++ b/compiler/utils/Maybes.lhs @@ -17,7 +17,7 @@ module Maybes ( expectJust, maybeToBool, - thenMaybe, seqMaybe, returnMaybe, failMaybe, fmapMMaybe + MaybeT(..) ) where import Data.Maybe @@ -77,31 +77,29 @@ mapCatMaybes f (x:xs) = case f x of Nothing -> mapCatMaybes f xs \end{code} -The Maybe monad -~~~~~~~~~~~~~~~ \begin{code} -seqMaybe :: Maybe a -> Maybe a -> Maybe a -seqMaybe (Just x) _ = Just x -seqMaybe Nothing my = my +orElse :: Maybe a -> a -> a +(Just x) `orElse` _ = x +Nothing `orElse` y = y +\end{code} -thenMaybe :: Maybe a -> (a -> Maybe b) -> Maybe b -thenMaybe ma mb = case ma of - Just x -> mb x - Nothing -> Nothing +%************************************************************************ +%* * +\subsection[MaybeT type]{The @MaybeT@ monad transformer} +%* * +%************************************************************************ -returnMaybe :: a -> Maybe a -returnMaybe = Just +\begin{code} -failMaybe :: Maybe a -failMaybe = Nothing +newtype MaybeT m a = MaybeT {runMaybeT :: m (Maybe a)} -orElse :: Maybe a -> a -> a -(Just x) `orElse` _ = x -Nothing `orElse` y = y +instance Functor m => Functor (MaybeT m) where + fmap f x = MaybeT $ fmap (fmap f) $ runMaybeT x -fmapMMaybe :: Monad m => (a -> m b) -> Maybe a -> m (Maybe b) -fmapMMaybe _ Nothing = return Nothing -fmapMMaybe f (Just x) = f x >>= \x' -> return (Just 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}