[Haskell-cafe] Help with identity functor print instance and monad transformers

Cody Goodman codygman.consulting at gmail.com
Tue Jan 20 05:09:34 UTC 2015

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?

If anyone could help me fix the code below as well as explain the questions
I have above it would be a great help.

import Control.Monad.Trans.State
import Control.Monad.IO.Class

type MyState = State Int

modAndPrintState :: Int -> MyState ()
modAndPrintState x = do
  get >>= \i -> do
    liftIO . print $ i
    if even x then put (x + i) else put (i * (x + 1) - x)

main = undefined

-- Prelude Control.Monad.Trans.State> :r
-- [1 of 1] Compiling Main             ( blah2.hs, interpreted )

-- blah2.hs:9:5:
--     No instance for (MonadIO Data.Functor.Identity.Identity)
--       arising from a use of `liftIO'
--     Possible fix:
--       add an instance declaration for
--       (MonadIO Data.Functor.Identity.Identity)
--     In the first argument of `(.)', namely `liftIO'
--     In the expression: liftIO . print
--     In a stmt of a 'do' block: liftIO . print $ i
-- Failed, modules loaded: none.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20150119/c5990730/attachment.html>

More information about the Haskell-Cafe mailing list