{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric #-}
#endif
#if __GLASGOW_HASKELL__ >= 710 && __GLASGOW_HASKELL__ < 802
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.Maybe (
MaybeT(..),
mapMaybeT,
hoistMaybe,
maybeToExceptT,
exceptToMaybeT,
liftCallCC,
liftCatch,
liftListen,
liftPass,
) where
import Control.Monad.IO.Class
import Control.Monad.Signatures
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except (ExceptT(..))
import Data.Functor.Classes
#if MIN_VERSION_base(4,12,0)
import Data.Functor.Contravariant
#endif
import Control.Applicative
import Control.Monad (MonadPlus(mzero, mplus), liftM)
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
import Control.Monad.Fix (MonadFix(mfix))
#if MIN_VERSION_base(4,4,0)
import Control.Monad.Zip (MonadZip(mzipWith))
#endif
import Data.Maybe (fromMaybe)
#if !(MIN_VERSION_base(4,8,0))
import Data.Foldable (Foldable(foldMap))
import Data.Traversable (Traversable(traverse))
#endif
#if __GLASGOW_HASKELL__ >= 704
import GHC.Generics
#endif
newtype MaybeT m a = MaybeT { MaybeT m a -> m (Maybe a)
runMaybeT :: m (Maybe a) }
#if __GLASGOW_HASKELL__ >= 710
deriving ((forall x. MaybeT m a -> Rep (MaybeT m a) x)
-> (forall x. Rep (MaybeT m a) x -> MaybeT m a)
-> Generic (MaybeT m a)
forall x. Rep (MaybeT m a) x -> MaybeT m a
forall x. MaybeT m a -> Rep (MaybeT m a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (m :: * -> *) a x. Rep (MaybeT m a) x -> MaybeT m a
forall (m :: * -> *) a x. MaybeT m a -> Rep (MaybeT m a) x
$cto :: forall (m :: * -> *) a x. Rep (MaybeT m a) x -> MaybeT m a
$cfrom :: forall (m :: * -> *) a x. MaybeT m a -> Rep (MaybeT m a) x
Generic, (forall a. MaybeT m a -> Rep1 (MaybeT m) a)
-> (forall a. Rep1 (MaybeT m) a -> MaybeT m a)
-> Generic1 (MaybeT m)
forall a. Rep1 (MaybeT m) a -> MaybeT m a
forall a. MaybeT m a -> Rep1 (MaybeT m) a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
forall (m :: * -> *) a.
Functor m =>
Rep1 (MaybeT m) a -> MaybeT m a
forall (m :: * -> *) a.
Functor m =>
MaybeT m a -> Rep1 (MaybeT m) a
$cto1 :: forall (m :: * -> *) a.
Functor m =>
Rep1 (MaybeT m) a -> MaybeT m a
$cfrom1 :: forall (m :: * -> *) a.
Functor m =>
MaybeT m a -> Rep1 (MaybeT m) a
Generic1)
#elif __GLASGOW_HASKELL__ >= 704
deriving (Generic)
#endif
instance (Eq1 m) => Eq1 (MaybeT m) where
liftEq :: (a -> b -> Bool) -> MaybeT m a -> MaybeT m b -> Bool
liftEq a -> b -> Bool
eq (MaybeT m (Maybe a)
x) (MaybeT m (Maybe b)
y) = (Maybe a -> Maybe b -> Bool) -> m (Maybe a) -> m (Maybe b) -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq ((a -> b -> Bool) -> Maybe a -> Maybe b -> Bool
forall (f :: * -> *) a b.
Eq1 f =>
(a -> b -> Bool) -> f a -> f b -> Bool
liftEq a -> b -> Bool
eq) m (Maybe a)
x m (Maybe b)
y
{-# INLINE liftEq #-}
instance (Ord1 m) => Ord1 (MaybeT m) where
liftCompare :: (a -> b -> Ordering) -> MaybeT m a -> MaybeT m b -> Ordering
liftCompare a -> b -> Ordering
comp (MaybeT m (Maybe a)
x) (MaybeT m (Maybe b)
y) = (Maybe a -> Maybe b -> Ordering)
-> m (Maybe a) -> m (Maybe b) -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare ((a -> b -> Ordering) -> Maybe a -> Maybe b -> Ordering
forall (f :: * -> *) a b.
Ord1 f =>
(a -> b -> Ordering) -> f a -> f b -> Ordering
liftCompare a -> b -> Ordering
comp) m (Maybe a)
x m (Maybe b)
y
{-# INLINE liftCompare #-}
instance (Read1 m) => Read1 (MaybeT m) where
liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (MaybeT m a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl = (String -> ReadS (MaybeT m a)) -> Int -> ReadS (MaybeT m a)
forall a. (String -> ReadS a) -> Int -> ReadS a
readsData ((String -> ReadS (MaybeT m a)) -> Int -> ReadS (MaybeT m a))
-> (String -> ReadS (MaybeT m a)) -> Int -> ReadS (MaybeT m a)
forall a b. (a -> b) -> a -> b
$
(Int -> ReadS (m (Maybe a)))
-> String
-> (m (Maybe a) -> MaybeT m a)
-> String
-> ReadS (MaybeT m a)
forall a t.
(Int -> ReadS a) -> String -> (a -> t) -> String -> ReadS t
readsUnaryWith ((Int -> ReadS (Maybe a))
-> ReadS [Maybe a] -> Int -> ReadS (m (Maybe a))
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS (Maybe a)
rp' ReadS [Maybe a]
rl') String
"MaybeT" m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT
where
rp' :: Int -> ReadS (Maybe a)
rp' = (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Maybe a)
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (f a)
liftReadsPrec Int -> ReadS a
rp ReadS [a]
rl
rl' :: ReadS [Maybe a]
rl' = (Int -> ReadS a) -> ReadS [a] -> ReadS [Maybe a]
forall (f :: * -> *) a.
Read1 f =>
(Int -> ReadS a) -> ReadS [a] -> ReadS [f a]
liftReadList Int -> ReadS a
rp ReadS [a]
rl
instance (Show1 m) => Show1 (MaybeT m) where
liftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> MaybeT m a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (MaybeT m (Maybe a)
m) =
(Int -> m (Maybe a) -> ShowS)
-> String -> Int -> m (Maybe a) -> ShowS
forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith ((Int -> Maybe a -> ShowS)
-> ([Maybe a] -> ShowS) -> Int -> m (Maybe a) -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> Maybe a -> ShowS
sp' [Maybe a] -> ShowS
sl') String
"MaybeT" Int
d m (Maybe a)
m
where
sp' :: Int -> Maybe a -> ShowS
sp' = (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Maybe a -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl
sl' :: [Maybe a] -> ShowS
sl' = (Int -> a -> ShowS) -> ([a] -> ShowS) -> [Maybe a] -> ShowS
forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl
instance (Eq1 m, Eq a) => Eq (MaybeT m a) where == :: MaybeT m a -> MaybeT m a -> Bool
(==) = MaybeT m a -> MaybeT m a -> Bool
forall (f :: * -> *) a. (Eq1 f, Eq a) => f a -> f a -> Bool
eq1
instance (Ord1 m, Ord a) => Ord (MaybeT m a) where compare :: MaybeT m a -> MaybeT m a -> Ordering
compare = MaybeT m a -> MaybeT m a -> Ordering
forall (f :: * -> *) a. (Ord1 f, Ord a) => f a -> f a -> Ordering
compare1
instance (Read1 m, Read a) => Read (MaybeT m a) where readsPrec :: Int -> ReadS (MaybeT m a)
readsPrec = Int -> ReadS (MaybeT m a)
forall (f :: * -> *) a. (Read1 f, Read a) => Int -> ReadS (f a)
readsPrec1
instance (Show1 m, Show a) => Show (MaybeT m a) where showsPrec :: Int -> MaybeT m a -> ShowS
showsPrec = Int -> MaybeT m a -> ShowS
forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
mapMaybeT :: (m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT :: (m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT m (Maybe a) -> n (Maybe b)
f = n (Maybe b) -> MaybeT n b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (n (Maybe b) -> MaybeT n b)
-> (MaybeT m a -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Maybe a) -> n (Maybe b)
f (m (Maybe a) -> n (Maybe b))
-> (MaybeT m a -> m (Maybe a)) -> MaybeT m a -> n (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT
{-# INLINE mapMaybeT #-}
hoistMaybe :: (Applicative m) => Maybe b -> MaybeT m b
hoistMaybe :: Maybe b -> MaybeT m b
hoistMaybe = m (Maybe b) -> MaybeT m b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe b) -> MaybeT m b)
-> (Maybe b -> m (Maybe b)) -> Maybe b -> MaybeT m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe b -> m (Maybe b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure
maybeToExceptT :: (Functor m) => e -> MaybeT m a -> ExceptT e m a
maybeToExceptT :: e -> MaybeT m a -> ExceptT e m a
maybeToExceptT e
e (MaybeT m (Maybe a)
m) = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> m (Either e a) -> ExceptT e m a
forall a b. (a -> b) -> a -> b
$ (Maybe a -> Either e a) -> m (Maybe a) -> m (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either e a -> (a -> Either e a) -> Maybe a -> Either e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> Either e a
forall a b. a -> Either a b
Left e
e) a -> Either e a
forall a b. b -> Either a b
Right) m (Maybe a)
m
{-# INLINE maybeToExceptT #-}
exceptToMaybeT :: (Functor m) => ExceptT e m a -> MaybeT m a
exceptToMaybeT :: ExceptT e m a -> MaybeT m a
exceptToMaybeT (ExceptT m (Either e a)
m) = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ (Either e a -> Maybe a) -> m (Either e a) -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((e -> Maybe a) -> (a -> Maybe a) -> Either e a -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe a -> e -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing) a -> Maybe a
forall a. a -> Maybe a
Just) m (Either e a)
m
{-# INLINE exceptToMaybeT #-}
instance (Functor m) => Functor (MaybeT m) where
fmap :: (a -> b) -> MaybeT m a -> MaybeT m b
fmap a -> b
f = (m (Maybe a) -> m (Maybe b)) -> MaybeT m a -> MaybeT m b
forall (m :: * -> *) a (n :: * -> *) b.
(m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT ((Maybe a -> Maybe b) -> m (Maybe a) -> m (Maybe b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f))
{-# INLINE fmap #-}
instance (Foldable f) => Foldable (MaybeT f) where
foldMap :: (a -> m) -> MaybeT f a -> m
foldMap a -> m
f (MaybeT f (Maybe a)
a) = (Maybe a -> m) -> f (Maybe a) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) f (Maybe a)
a
{-# INLINE foldMap #-}
instance (Traversable f) => Traversable (MaybeT f) where
traverse :: (a -> f b) -> MaybeT f a -> f (MaybeT f b)
traverse a -> f b
f (MaybeT f (Maybe a)
a) = f (Maybe b) -> MaybeT f b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (f (Maybe b) -> MaybeT f b) -> f (f (Maybe b)) -> f (MaybeT f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Maybe a -> f (Maybe b)) -> f (Maybe a) -> f (f (Maybe b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((a -> f b) -> Maybe a -> f (Maybe b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) f (Maybe a)
a
{-# INLINE traverse #-}
instance (Functor m, Monad m) => Applicative (MaybeT m) where
pure :: a -> MaybeT m a
pure = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a)
-> (a -> m (Maybe a)) -> a -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> (a -> Maybe a) -> a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just
{-# INLINE pure #-}
MaybeT m (a -> b)
mf <*> :: MaybeT m (a -> b) -> MaybeT m a -> MaybeT m b
<*> MaybeT m a
mx = m (Maybe b) -> MaybeT m b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe b) -> MaybeT m b) -> m (Maybe b) -> MaybeT m b
forall a b. (a -> b) -> a -> b
$ do
Maybe (a -> b)
mb_f <- MaybeT m (a -> b) -> m (Maybe (a -> b))
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m (a -> b)
mf
case Maybe (a -> b)
mb_f of
Maybe (a -> b)
Nothing -> Maybe b -> m (Maybe b)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a -> b
f -> do
Maybe a
mb_x <- MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
mx
case Maybe a
mb_x of
Maybe a
Nothing -> Maybe b -> m (Maybe b)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a
x -> Maybe b -> m (Maybe b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f a
x))
{-# INLINE (<*>) #-}
MaybeT m a
m *> :: MaybeT m a -> MaybeT m b -> MaybeT m b
*> MaybeT m b
k = MaybeT m a
m MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
_ -> MaybeT m b
k
{-# INLINE (*>) #-}
instance (Functor m, Monad m) => Alternative (MaybeT m) where
empty :: MaybeT m a
empty = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)
{-# INLINE empty #-}
MaybeT m a
x <|> :: MaybeT m a -> MaybeT m a -> MaybeT m a
<|> MaybeT m a
y = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ do
Maybe a
v <- MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
x
case Maybe a
v of
Maybe a
Nothing -> MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
y
Just a
_ -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
v
{-# INLINE (<|>) #-}
instance (Monad m) => Monad (MaybeT m) where
#if !(MIN_VERSION_base(4,8,0))
return = MaybeT . return . Just
{-# INLINE return #-}
#endif
MaybeT m a
x >>= :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b
>>= a -> MaybeT m b
f = m (Maybe b) -> MaybeT m b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe b) -> MaybeT m b) -> m (Maybe b) -> MaybeT m b
forall a b. (a -> b) -> a -> b
$ do
Maybe a
v <- MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
x
case Maybe a
v of
Maybe a
Nothing -> Maybe b -> m (Maybe b)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a
y -> MaybeT m b -> m (Maybe b)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (a -> MaybeT m b
f a
y)
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail _ = MaybeT (return Nothing)
{-# INLINE fail #-}
#endif
#if MIN_VERSION_base(4,9,0)
instance (Monad m) => Fail.MonadFail (MaybeT m) where
fail :: String -> MaybeT m a
fail String
_ = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)
{-# INLINE fail #-}
#endif
instance (Monad m) => MonadPlus (MaybeT m) where
mzero :: MaybeT m a
mzero = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing)
{-# INLINE mzero #-}
mplus :: MaybeT m a -> MaybeT m a -> MaybeT m a
mplus MaybeT m a
x MaybeT m a
y = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ do
Maybe a
v <- MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
x
case Maybe a
v of
Maybe a
Nothing -> MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
y
Just a
_ -> Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
v
{-# INLINE mplus #-}
instance (MonadFix m) => MonadFix (MaybeT m) where
mfix :: (a -> MaybeT m a) -> MaybeT m a
mfix a -> MaybeT m a
f = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT ((Maybe a -> m (Maybe a)) -> m (Maybe a)
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT m a -> m (Maybe a))
-> (Maybe a -> MaybeT m a) -> Maybe a -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> MaybeT m a
f (a -> MaybeT m a) -> (Maybe a -> a) -> Maybe a -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
forall a. a
bomb))
where bomb :: a
bomb = String -> a
forall a. HasCallStack => String -> a
error String
"mfix (MaybeT): inner computation returned Nothing"
{-# INLINE mfix #-}
instance MonadTrans MaybeT where
lift :: m a -> MaybeT m a
lift = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a)
-> (m a -> m (Maybe a)) -> m a -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe a) -> m a -> m (Maybe a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Maybe a
forall a. a -> Maybe a
Just
{-# INLINE lift #-}
instance (MonadIO m) => MonadIO (MaybeT m) where
liftIO :: IO a -> MaybeT m a
liftIO = m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> MaybeT m a) -> (IO a -> m a) -> IO a -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
{-# INLINE liftIO #-}
#if MIN_VERSION_base(4,4,0)
instance (MonadZip m) => MonadZip (MaybeT m) where
mzipWith :: (a -> b -> c) -> MaybeT m a -> MaybeT m b -> MaybeT m c
mzipWith a -> b -> c
f (MaybeT m (Maybe a)
a) (MaybeT m (Maybe b)
b) = m (Maybe c) -> MaybeT m c
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe c) -> MaybeT m c) -> m (Maybe c) -> MaybeT m c
forall a b. (a -> b) -> a -> b
$ (Maybe a -> Maybe b -> Maybe c)
-> m (Maybe a) -> m (Maybe b) -> m (Maybe c)
forall (m :: * -> *) a b c.
MonadZip m =>
(a -> b -> c) -> m a -> m b -> m c
mzipWith ((a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f) m (Maybe a)
a m (Maybe b)
b
{-# INLINE mzipWith #-}
#endif
#if MIN_VERSION_base(4,12,0)
instance Contravariant m => Contravariant (MaybeT m) where
contramap :: (a -> b) -> MaybeT m b -> MaybeT m a
contramap a -> b
f = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a)
-> (MaybeT m b -> m (Maybe a)) -> MaybeT m b -> MaybeT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> Maybe b) -> m (Maybe b) -> m (Maybe a)
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap ((a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (Maybe b) -> m (Maybe a))
-> (MaybeT m b -> m (Maybe b)) -> MaybeT m b -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MaybeT m b -> m (Maybe b)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT
{-# INLINE contramap #-}
#endif
liftCallCC :: CallCC m (Maybe a) (Maybe b) -> CallCC (MaybeT m) a b
liftCallCC :: CallCC m (Maybe a) (Maybe b) -> CallCC (MaybeT m) a b
liftCallCC CallCC m (Maybe a) (Maybe b)
callCC (a -> MaybeT m b) -> MaybeT m a
f =
m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ CallCC m (Maybe a) (Maybe b)
callCC CallCC m (Maybe a) (Maybe b) -> CallCC m (Maybe a) (Maybe b)
forall a b. (a -> b) -> a -> b
$ \ Maybe a -> m (Maybe b)
c -> MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT ((a -> MaybeT m b) -> MaybeT m a
f (m (Maybe b) -> MaybeT m b
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe b) -> MaybeT m b)
-> (a -> m (Maybe b)) -> a -> MaybeT m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> m (Maybe b)
c (Maybe a -> m (Maybe b)) -> (a -> Maybe a) -> a -> m (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just))
{-# INLINE liftCallCC #-}
liftCatch :: Catch e m (Maybe a) -> Catch e (MaybeT m) a
liftCatch :: Catch e m (Maybe a) -> Catch e (MaybeT m) a
liftCatch Catch e m (Maybe a)
f MaybeT m a
m e -> MaybeT m a
h = m (Maybe a) -> MaybeT m a
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (m (Maybe a) -> MaybeT m a) -> m (Maybe a) -> MaybeT m a
forall a b. (a -> b) -> a -> b
$ Catch e m (Maybe a)
f (MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
m) (MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT m a -> m (Maybe a))
-> (e -> MaybeT m a) -> e -> m (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> MaybeT m a
h)
{-# INLINE liftCatch #-}
liftListen :: (Monad m) => Listen w m (Maybe a) -> Listen w (MaybeT m) a
liftListen :: Listen w m (Maybe a) -> Listen w (MaybeT m) a
liftListen Listen w m (Maybe a)
listen = (m (Maybe a) -> m (Maybe (a, w))) -> Listen w (MaybeT m) a
forall (m :: * -> *) a (n :: * -> *) b.
(m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT ((m (Maybe a) -> m (Maybe (a, w))) -> Listen w (MaybeT m) a)
-> (m (Maybe a) -> m (Maybe (a, w))) -> Listen w (MaybeT m) a
forall a b. (a -> b) -> a -> b
$ \ m (Maybe a)
m -> do
(Maybe a
a, w
w) <- Listen w m (Maybe a)
listen m (Maybe a)
m
Maybe (a, w) -> m (Maybe (a, w))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, w) -> m (Maybe (a, w)))
-> Maybe (a, w) -> m (Maybe (a, w))
forall a b. (a -> b) -> a -> b
$! (a -> (a, w)) -> Maybe a -> Maybe (a, w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ a
r -> (a
r, w
w)) Maybe a
a
{-# INLINE liftListen #-}
liftPass :: (Monad m) => Pass w m (Maybe a) -> Pass w (MaybeT m) a
liftPass :: Pass w m (Maybe a) -> Pass w (MaybeT m) a
liftPass Pass w m (Maybe a)
pass = (m (Maybe (a, w -> w)) -> m (Maybe a)) -> Pass w (MaybeT m) a
forall (m :: * -> *) a (n :: * -> *) b.
(m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT ((m (Maybe (a, w -> w)) -> m (Maybe a)) -> Pass w (MaybeT m) a)
-> (m (Maybe (a, w -> w)) -> m (Maybe a)) -> Pass w (MaybeT m) a
forall a b. (a -> b) -> a -> b
$ \ m (Maybe (a, w -> w))
m -> Pass w m (Maybe a)
pass Pass w m (Maybe a) -> Pass w m (Maybe a)
forall a b. (a -> b) -> a -> b
$ do
Maybe (a, w -> w)
a <- m (Maybe (a, w -> w))
m
(Maybe a, w -> w) -> m (Maybe a, w -> w)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe a, w -> w) -> m (Maybe a, w -> w))
-> (Maybe a, w -> w) -> m (Maybe a, w -> w)
forall a b. (a -> b) -> a -> b
$! case Maybe (a, w -> w)
a of
Maybe (a, w -> w)
Nothing -> (Maybe a
forall a. Maybe a
Nothing, w -> w
forall a. a -> a
id)
Just (a
v, w -> w
f) -> (a -> Maybe a
forall a. a -> Maybe a
Just a
v, w -> w
f)
{-# INLINE liftPass #-}