Skip to content

Instantly share code, notes, and snippets.

@hatashiro
Last active May 2, 2016 16:18
Show Gist options
  • Select an option

  • Save hatashiro/31a9deb2d45feb78336919ec602234aa to your computer and use it in GitHub Desktop.

Select an option

Save hatashiro/31a9deb2d45feb78336919ec602234aa to your computer and use it in GitHub Desktop.
A monad transformer for Identity
import Control.Applicative
import Control.Monad
import Control.Monad.Trans.Class
import Data.Functor.Identity
newtype IdentityT m a = IdentityT { runIdentityT :: m (Identity a) }
instance Functor f => Functor (IdentityT f) where
fmap f = IdentityT . fmap (fmap f) . runIdentityT
instance Applicative a => Applicative (IdentityT a) where
pure = IdentityT . pure . pure
f <*> a = IdentityT $ liftA2 (<*>) (runIdentityT f) (runIdentityT a)
instance Monad m => Monad (IdentityT m) where
return = IdentityT . return . Identity
x >>= f = IdentityT $ runIdentityT x >>= (runIdentityT . f . runIdentity)
instance MonadTrans IdentityT where
lift m = IdentityT (liftM Identity m)
testT :: IdentityT IO String
testT = lift getLine
main :: IO ()
main = do
a <- runIdentityT testT
putStrLn $ runIdentity a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment