Commit f2b752da authored by Steffen Michels's avatar Steffen Michels Committed by Mart Lubbers

add MaybeT

parent 564be448
......@@ -9,6 +9,7 @@ from StdOverloaded import class ==(..)
from Data.Functor import class Functor
from Control.Applicative import class Applicative, class *>, class <*, class Alternative
from Control.Monad import class Monad, class MonadPlus
from Control.Monad.Trans import class MonadTrans
from Data.Monoid import class Semigroup, class Monoid
from Data.Foldable import class Foldable
from Data.Traversable import class Traversable
......@@ -46,3 +47,28 @@ maybeSt :: *st (.a *st -> *st) !(Maybe .a) -> *st
* Directly return a Just value or return a default value if the argument is a Nothing value.
*/
fromMaybe :: .a !(Maybe .a) -> .a
/**
* The Maybe monad transformer.
*/
:: MaybeT m a = MaybeT !(m (Maybe a))
/**
* Runs a MaybeT as the monad wrapped inside the transformer.
*/
runMaybeT :: !(MaybeT m a) -> m (Maybe a)
/**
* Transforms the computation inside a transformer.
*
* @param The computation transformation.
* @param The transformer to be transformed.
* @result The transformed transformer.
*/
mapMaybeT :: !((m (Maybe a)) -> n (Maybe b)) !(MaybeT m a) -> MaybeT n b
instance Functor (MaybeT m) | Functor m
instance Applicative (MaybeT m) | Monad m
instance Alternative (MaybeT m) | Monad m
instance Monad (MaybeT m) | Monad m
instance MonadTrans MaybeT
......@@ -6,11 +6,12 @@ import StdFunctions
import StdMisc
import Data.Functor
import Data.Monoid
import Data.Func
from Data.Foldable import class Foldable(..)
from Data.Traversable import class Traversable(traverse)
import qualified Data.Traversable as T
import Control.Applicative
import Control.Monad
import Control.Monad, Control.Monad.Trans
import Data.GenEq
instance Functor Maybe where fmap f m = mapMaybe f m
......@@ -105,3 +106,43 @@ maybeSt st f (Just x) = f x st
fromMaybe :: .a !(Maybe .a) -> .a
fromMaybe x mb = maybe x id mb
runMaybeT :: !(MaybeT m a) -> m (Maybe a)
runMaybeT (MaybeT f) = f
mapMaybeT :: !((m (Maybe a)) -> n (Maybe b)) !(MaybeT m a) -> MaybeT n b
mapMaybeT f m = MaybeT $ f $ runMaybeT m
instance Functor (MaybeT m) | Functor m where
fmap f m = mapMaybeT (fmap $ fmap f) m
instance Applicative (MaybeT m) | Monad m where
pure x = MaybeT $ pure $ Just x
<*> mf mx = MaybeT $
runMaybeT mf >>= \mb_f ->
case mb_f of
Nothing = pure Nothing
Just f =
runMaybeT mx >>= \mb_x ->
case mb_x of
Nothing = pure Nothing
Just x = pure $ Just $ f x
instance Alternative (MaybeT m) | Monad m where
empty = MaybeT $ return Nothing
<|> x y = MaybeT $
runMaybeT x >>= \v ->
case v of
Nothing -> runMaybeT y
Just _ -> return v
instance Monad (MaybeT m) | Monad m where
bind x f = MaybeT $
runMaybeT x >>= \v ->
case v of
Nothing -> return Nothing
Just y -> runMaybeT $ f y
instance MonadTrans MaybeT where
liftT m = MaybeT $ liftM Just m
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment