Help with identity functor print instance and monad transformers

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. Thanks!

On Tue, Jan 20, 2015 at 12:09 PM, Cody Goodman < codygman.consulting@gmail.com> wrote:
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)
This code works in the State monad and the only effect there is what it says on the tin: state, not IO. So the code gets use of put and get, but not print. Others will soon chime in on StateT solutions, but from the looks of it, you merely want to display the trace of the state. In which case look to Debug.Trace. -- Kim-Ee

On Tue, Jan 20, 2015 at 12:09 AM, Cody Goodman
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

Thanks for the help!
On Jan 22, 2015 5:03 PM, "wren romano"
On Tue, Jan 20, 2015 at 12:09 AM, Cody Goodman
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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Cody Goodman
-
Kim-Ee Yeoh
-
wren romano