Last active
May 2, 2016 16:18
-
-
Save hatashiro/31a9deb2d45feb78336919ec602234aa to your computer and use it in GitHub Desktop.
A monad transformer for Identity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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