为什么Haskell中没有IO变压器? [英] Why is there no IO transformer in Haskell?

查看:87
本文介绍了为什么Haskell中没有IO变压器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一个monad都带有一个变压器版本,从我所知道的变压器的想法是monads的通用扩展。按照其他变换器的构建方式, IOT 可能类似于

  newtype IOT ma = IOT {runIOT :: m(IO a)} 

有用的应用程序: IOT Maybe 既可以执行IO操作也可以不执行任何操作, IOT [] 可以创建列表那么为什么Haskell中没有IO变换器呢?

$ 序列 d。



b
$ b

(注:我见过在Haskell Cafe上发布这篇文章,但无法理解它。另外, ST转换器的Hackage页面在其描述中提到了一个可能相关的问题,但没有提供任何细节。)

解决方案

考虑具体的e IOT可能的xample。你会如何为此写一个 Monad 实例?你可以从这样的事情开始:

pre $实例Monad(IOT可能)其中
return x = IOT(Just(返回x))
IOT Nothing>> = _ = IOT Nothing
IOT(Just m)>> = k = IOT $ errorwhat now?
其中m'= liftM(runIOT。k)m

现在你有 m':: IO(Maybe(IO b)),但是你需要类型 Maybe(IO b) - 最重要的是 - Just Nothing 之间的选择应该由 m'。那将如何实施?



当然,答案是不会的,因为它不行。你也不能证明那里存在 unsafePerformIO ,隐藏在一个纯粹的界面之后,因为你基本上要求一个纯粹的值 - 选择 Maybe 构造函数 - 取决于 IO 中的结果。 Nnnnnope,不会发生。



在一般情况下情况更糟,因为任意(通用量化) Monad




更难解开。 >顺便说一句,你提到的 ST 变换器的实现方式与你建议的 IOT 不同。它使用内部实现 ST 作为 State -like monad,使用 magic pixie dust 由编译器提供的特殊原语,并基于此定义一个 StateT 类型的变换器。 IO 在内部实现为更神奇的 ST ,所以假设的 IOT 可以用类似的方式定义。



并不是说这实际上改变了任何东西,除了可能让您更好地控制不纯副作用的相对顺序由 IOT 造成。


Every other monad comes with a transformer version, and from what I know the idea of a transformer is a generic extension of monads. Following how the other transformers are build, IOT would be something like

newtype IOT m a = IOT { runIOT :: m (IO a) }

for which I could make up useful applications on the spot: IOT Maybe can either do an IO action or nothing, IOT [] can build a list that can later be sequenced.

So why is there no IO transformer in Haskell?

(Notes: I've seen this post on Haskell Cafe, but can't make much sense of it. Also, the Hackage page for the ST transformer mentions a possibly related issue in its description, but doesn't offer any details.)

解决方案

Consider the specific example of IOT Maybe. How would you write a Monad instance for that? You could start with something like this:

instance Monad (IOT Maybe) where
    return x = IOT (Just (return x))
    IOT Nothing >>= _ = IOT Nothing
    IOT (Just m) >>= k = IOT $ error "what now?"
      where m' = liftM (runIOT . k) m

Now you have m' :: IO (Maybe (IO b)), but you need something of type Maybe (IO b), where--most importantly--the choice between Just and Nothing should be determined by m'. How would that be implemented?

The answer, of course, is that it wouldn't, because it can't. Nor can you justify an unsafePerformIO in there, hidden behind a pure interface, because fundamentally you're asking for a pure value--the choice of Maybe constructor--to depend on the result of something in IO. Nnnnnope, not gonna happen.

The situation is even worse in the general case, because an arbitrary (universally quantified) Monad is even more impossible to unwrap than IO is.


Incidentally, the ST transformer you mention is implemented differently from your suggested IOT. It uses the internal implementation of ST as a State-like monad using magic pixie dust special primitives provided by the compiler, and defines a StateT-like transformer based on that. IO is implemented internally as an even more magical ST, and so a hypothetical IOT could be defined in a similar way.

Not that this really changes anything, other than possibly giving you better control over the relative ordering of impure side effects caused by IOT.

这篇关于为什么Haskell中没有IO变压器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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