将StateT IO与State结合起来 [英] Combining StateT IO with State

查看:95
本文介绍了将StateT IO与State结合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个函数 f :: State Int(),是否可以在另一个函数中使用 g :: StateT Int IO ()?用嵌套它= f = do {something; g} 无法使用进行类型检查无法将类型'Data.Functor.Identity.Identity'与'IO'匹配

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

推荐答案

是的,这个操作通常被称为提升。只需定义并使用以下函数:

Yes, this operation is usually called "hoisting". Just define and use the following function:

hoistState :: Monad m => State s a -> StateT s m a
hoistState = StateT . (return .) . runState

不幸的是,它没有在 Control.Monad.State 模块。

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

这篇关于将StateT IO与State结合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆