aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/26/MaybeT/src/Identity.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Haskell-book/26/MaybeT/src/Identity.hs')
-rw-r--r--Haskell-book/26/MaybeT/src/Identity.hs43
1 files changed, 43 insertions, 0 deletions
diff --git a/Haskell-book/26/MaybeT/src/Identity.hs b/Haskell-book/26/MaybeT/src/Identity.hs
new file mode 100644
index 0000000..8189c18
--- /dev/null
+++ b/Haskell-book/26/MaybeT/src/Identity.hs
@@ -0,0 +1,43 @@
+module Identity where
+
+import MonadIO
+import MonadTrans
+
+newtype Identity a =
+ Identity { runIdentity :: a }
+ deriving (Eq, Show)
+
+instance Functor Identity where
+ fmap f (Identity a) = Identity (f a)
+
+instance Applicative Identity where
+ pure = Identity
+ (Identity f) <*> (Identity a) = Identity (f a)
+
+newtype IdentityT f a =
+ IdentityT { runIdentityT :: f a }
+ deriving (Eq, Show)
+
+instance (Functor m)
+ => Functor (IdentityT m) where
+ fmap f (IdentityT fa) = IdentityT (fmap f fa)
+
+instance (Applicative m)
+ => Applicative (IdentityT m) where
+ pure x = IdentityT (pure x)
+
+ (IdentityT fab) <*> (IdentityT fa) =
+ IdentityT (fab <*> fa)
+
+instance (Monad m)
+ => Monad (IdentityT m) where
+ return = pure
+
+ (IdentityT ma) >>= f = IdentityT $ ma >>= runIdentityT . f
+
+instance (MonadIO m)
+ => MonadIO (IdentityT m) where
+ liftIO = IdentityT . liftIO
+
+instance MonadTrans IdentityT where
+ lift = IdentityT