summaryrefslogtreecommitdiff
path: root/Haskell-book/26/MaybeT/src/Identity.hs
blob: 8189c18dc7997c22992dfe73699d412ef0629732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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