Skip to content

Commit

Permalink
Functor, Applkicative, Monad instance for MaybeT transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasios Valtinos committed Nov 3, 2022
1 parent 243840f commit 53373f5
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,24 @@ instance Applicative (EitherX e) where
instance Monad (EitherX e) where
return = RightX
RightX m >>= k = k m
LeftX e >>= _ = LeftX e
LeftX e >>= _ = LeftX e


newtype MaybeTX m a = MaybeTX { runMaybeTX :: m (Maybe a)}

instance Functor m => Functor (MaybeTX m) where
fmap f (MaybeTX ma) = MaybeTX $ (fmap . fmap) f ma

instance Applicative m => Applicative (MaybeTX m) where
pure x = MaybeTX (pure (pure x))
MaybeTX rfa <*> MaybeTX rxa = MaybeTX $ (<*>) <$> rfa <*> rxa

instance Monad m => Monad (MaybeTX m) where
return x = MaybeTX $ return (Just x)
MaybeTX ma >>= f = MaybeTX $ do
maybeValue <- ma
case maybeValue of
Nothing -> return Nothing
Just y -> runMaybeTX (f y)


0 comments on commit 53373f5

Please sign in to comment.