Fraser
Fraser

Reputation: 1521

Combining StateT IO with State

If I have a function f :: State Int (), is it possible to use it within another function g :: StateT Int IO ()? Nesting it with f = do { something; g } fails to typecheck with Couldn't match type 'Data.Functor.Identity.Identity' with 'IO'.

Upvotes: 12

Views: 1191

Answers (1)

Roman Cheplyaka
Roman Cheplyaka

Reputation: 38708

Yes, this operation is usually called "hoisting". For the State monad, it could be defined as

hoistState :: Monad m => State s a -> StateT s m a
hoistState = state . runState

Unfortunately, it is not defined in the Control.Monad.State module.

Upvotes: 22

Related Questions