aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/26/MaybeT/src/Identity.hs
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
committerEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
commit98329e0a3dd4f78b5d815ac3896272ec70904901 (patch)
tree80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/26/MaybeT/src/Identity.hs
parent3624c712d72d246f21d4e710cec7c11e052e0326 (diff)
downloadbook-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz
Add remaining haskell book exercises
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