-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anastasios Valtinos
committed
Oct 13, 2022
1 parent
0ae4df2
commit 64289ae
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Haskell Excercises & Code/Chapter25 - Composing Types/transformers.hs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module MonadT where | ||
|
||
newtype Identity a = | ||
Identity { runIdentity :: a } | ||
deriving (Eq, Show) | ||
|
||
-- newtype IdentityN a = IdentityN a deriving (Eq, Show) | ||
|
||
-- Monad Transformer | ||
-- The identity monad transformer, what changed here is that we added one extra type argument | ||
newtype IdentityT f a = | ||
IdentityT { runIdentityT :: f a } | ||
deriving (Eq, Show) | ||
|
||
instance Functor Identity where | ||
fmap f (Identity a) = Identity (f a) | ||
|
||
instance (Functor m) => Functor (IdentityT m) where | ||
fmap f (IdentityT fa) = IdentityT (fmap f fa) | ||
|
||
instance Applicative Identity where | ||
pure = Identity | ||
(Identity f) <*> (Identity a) = Identity (f a) | ||
|
||
instance (Applicative m) => Applicative (IdentityT m) where | ||
pure x = IdentityT (pure x) | ||
(IdentityT fab) <*> (IdentityT fa) = IdentityT (fab <*> fa) | ||
|
||
instance Monad Identity where | ||
return = pure | ||
(Identity a) >>= f = f a | ||
|
||
instance (Monad m) => Monad (IdentityT m) where | ||
return = pure | ||
(IdentityT ma) >>= f = IdentityT $ ma >>= runIdentityT . f | ||
-- breakdown the types | ||
--f :: a -> m b | ||
-- ma :: Monadic a | ||
-- runIdentityT :: IdentityT m a -> m a | ||
-- f :: a -> f a |