为什么Haskell没有I Monad(仅用于输入,与IO monad不同)? [英] Why does Haskell not have an I Monad (for input only, unlike the IO monad)?

查看:138
本文介绍了为什么Haskell没有I Monad(仅用于输入,与IO monad不同)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概念上,执行输出的计算似乎与仅执行输入的计算有很大不同。从某种意义上说,后者是更纯粹的。



我想,有一种方法可以将我的程序的输入部分与输入部分分开可能实际上写了一些东西。



那么,为什么没有输入只有Monad?



不会有我的monad(和一个O Monad,可以组合成IO Monad)吗?



编辑:I通常意味着输入为阅读文件,而不是与用户交互。这也是我的用例,我可以假设输入文件在程序执行过程中不会改变(否则,可能会导致未定义的行为)。 解决方案

我不同意bdonlan的回答。的确,输入和输出都不是更纯的,但它们之间的差别很大。把IO作为单一的罪恶仓来评判是非常有效的,因为所有效果都被挤在一起,而且它确实可以确保某些属性更难。例如,如果有许多函数只能从某些内存位置读取,并且哪些函数永远不会导致这些位置被更改,那么知道您可以重新排序它们的执行顺序是很好的。或者如果你有一个使用forkIO和MVars的程序,根据它的类型知道它不是读取/ etc / passwd会很好。



此外,一个可以以除叠加变形金刚之外的方式组合单体效果。你不能用所有 monad(只是免费的monad)来做到这一点,但对于这样的情况,这是你真正需要的。例如,iospec软件包提供了一个纯粹的IO规范 - 它不会独立读写,但它可以与STM,MVars,forkIO,soforth分离。



http://hackage.haskell.org/package/IOSpec



数据类型单点菜单(很棒的阅读,非常有影响力,不能推荐,等等。)。

Conceptually, it seems that a computation that performs output is very different from one that performs input only. The latter is, in one sense, much purer.

I, for one, would like to have a way to separate the input only parts of my programme from the ones that might actually write something out.

So, why is there no input only Monad?

Any reason why it wouldn't work to have an I monad (and an O Monad, which could be combined into the IO Monad)?

Edit: I mostly meant input as reading files, not interacting with the user. This is also my use case, where I can assume that input files do not change during the execution of the programme (otherwise, it's fine to get undefined behaviour).

解决方案

I disagree with bdonlan's answer. It's true that neither input nor output are more "pure" but they are quite different. It's quite valid to critique IO as the single "sin bin" where all effects get crammed together, and it does make ensuring certain properties harder. For example, if you have many functions that you know only read from certain memory locations, and which could never cause those locations to be altered, it would be nice to know that you can reorder their execution. Or if you have a program that uses forkIO and MVars, it would be nice to know, based on its type, that it isn't also reading /etc/passwd.

Furthermore, one can compose monadic effects in a fashion besides just stacked transformers. You can't do this with all monads (just free monads), but for a case like this that's all you really need. The iospec package, for example, provides a pure specification of IO -- it doesn't seperate reading and writing, but it does seperate them from, e.g., STM, MVars, forkIO, soforth.

http://hackage.haskell.org/package/IOSpec

The key ideas for how you can combine the different monads cleanly are described in the Data Types a la Carte paper (great reading, very influential, can't recommend enough, etc.etc.).

这篇关于为什么Haskell没有I Monad(仅用于输入,与IO monad不同)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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