Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining StateT IO with State

Tags:

haskell

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'.

like image 911
Fraser Avatar asked Jun 26 '13 16:06

Fraser


1 Answers

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.

like image 121
Roman Cheplyaka Avatar answered Sep 27 '22 22:09

Roman Cheplyaka