Combining StateT IO with State

喜欢而已 提交于 2020-01-21 01:38:05

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/17325485/combining-statet-io-with-state

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!