[Haskell-cafe] Help with identity functor print instance and monad transformers
wren romano
winterkoninkje at gmail.com
Thu Jan 22 23:03:41 UTC 2015
On Tue, Jan 20, 2015 at 12:09 AM, Cody Goodman
<codygman.consulting at gmail.com> wrote:
> I understand the error below, but I'm not really sure what an instance of
> liftIO would look like for the Identity functor. I guess I'm not all to
> clear on what an identity functor is.
>
> I'm a little fuzzy on what an identity monad is. I understand that id gives
> the identity of something back pretty well though. Can anyone help?
The identity functor/monad is this:
newtype Identity a = Identity { runIdentity :: a }
That is, the type constructor `Identity` works like the function `id`,
but on the type level. It doesn't add any sort of special effects, it
just gives us a type constructor that we can use whenever we need
something of kind * -> *, like when working with monad transformer
stacks.
Identity is trivially a monad:
instance Functor Identity where
fmap f (Identity x) = Identity (f x)
instance Applicative Identity where
pure x = Identity x
Identity f <*> Identity x = Identity (f x)
instance Monad Identity where
return x = Identity x
Identity x >>= f = f x
If we erase all the newtype un/wrapping, then we can see that all
these functions are just variations on ($) and `id`. That's why we say
it's trivial.
--
Live well,
~wren
More information about the Haskell-Cafe
mailing list