关于Haskell IO Monad中的(何时执行) [英] Regarding (When Executed) in Haskell IO Monad

查看:109
本文介绍了关于Haskell IO Monad中的(何时执行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IO Monad没有问题。但我想了解以下内容:


  1. 在所有/几乎Haskell教程/教科书中,他们一直说getChar不是纯函数,因为它可以给你不同的结果。我的问题是:谁说这首先是一个功能。除非你给我实现这个功能,并且我研究这个实现,我不能保证它是纯粹的。那么,该实现在哪里?

  2. 在所有/几乎Haskell教程/教科书中,据说(IO字符串)是一个动作(当执行时)它可以给你一个String类型的值。这很好,但是这个执行发生在谁/何处。当然!计算机正在执行此操作。这也可以。但由于我只是一个初学者,我希望你原谅我问,这个执行的秘诀在哪里。我猜想它不是用Haskell编写的。毕竟,这个后来的想法是否意味着一个Haskell程序被转换成一个类C程序,最终将被转换为Assembly-> Machine code?如果是这样,那么可以在Haskell中找到IO的实现?

非常感谢
<解决方案Haskell函数与 computations 不同。



计算是一个命令性的代码(可能用C或汇编语言编写,然后编译为机器代码,可直接在处理器上执行),这本质上是有效的,甚至不受其影响。也就是说,一旦运行,计算就可以访问和修改任何内存并执行任何操作,例如与键盘和屏幕交互,甚至发射导弹。

通过相比之下,像Haskell这样的纯语言功能无法改变任意内存和发射导弹。它只能改变它自己的内存部分,并返回它的类型中指定的结果。



因此,从某种意义上说,Haskell是一种语言,不能做任何事情。 Haskell无用。这是1990年代的一个主要问题,直到 IO 被集成到Haskell中。



现在,一个 IO a value是链接到单独准备的计算,最终希望产生 a 。您将无法使用纯粹的Haskell函数创建 IO a 。所有的 IO 原语都是单独设计的,并打包到GHC中。然后,你可以将这些简单的计算组合成更简单的计算,最终你的程序可能会有你想要的任何效果。

然而,有一点是纯函数是彼此分开,如果你一起使用它们,它们只能互相影响。另一方面,计算可以自由地相互作用(正如我所说的,它们通常可以做任何事情),因此可以(并且确实)意外地破坏对方。这就是为什么用命令式语言编写的软件有这么多错误!因此,在Haskell中,计算保存在 IO 中。



我希望这可以消除至少一些混淆。

I have no problem with the IO Monad. But I want to understand the followings:

  1. In All/almost Haskell tutorials/ text books they keep saying that getChar is not a pure function, because it can give you a different result. My question is: Who said that this is a function in the first place. Unless you give me the implementation of this function, and I study that implementation, I can't guarantee it is pure. So, where is that implementation?
  2. In All/almost Haskell tutorials/ text books, it's said that, say (IO String) is an action that (When executed) it can give you back a value of type String. This is fine, but who/where this execution is taking place. Of course! The computer is doing this execution. This is OK too. but since I am only a beginner, I hope you forgive me to ask, where is the recipe for this "execution". I would guess it is not written in Haskell. Does this later idea mean that, after all, that a Haskell program is converted into a C-like program, which will eventually be converted into Assembly -> Machine code? If so, where one can find the implementation of the IO stuff in Haskell?

Many thanks

解决方案

Haskell functions are not the same as computations.

A computation is a piece of imperative code (perhaps written in C or Assembler, and then compiled to machine code, directly executable on a processor), that is by nature effectful and even unrestricted in its effects. That is, once it is ran, a computation may access and alter any memory and perform any operations, such as interacting with keyboard and screen, or even launching missiles.

By contrast, a function in a pure language, such as Haskell, is unable to alter arbitrary memory and launch missiles. It can only alter its own personal section of memory and return a result that is specified in its type.

So, in a sense, Haskell is a language that cannot do anything. Haskell is useless. This was a major problem during the 1990's, until IO was integrated into Haskell.

Now, an IO a value is a link to a separately prepared computation that will, eventually, hopefully, produce a. You will not be able to create an IO a out of pure Haskell functions. All the IO primitives are designed separately, and packaged into GHC. You can then compose these simple computations into less trivial ones, and eventually your program may have any effects you may wish.

One point, though: pure functions are separate from each other, they can only influence each other if you use them together. Computations, on the other hand, may interact with each other freely (as I said, they can generally do anything), and therefore can (and do) accidentally break each other. That's why there are so many bugs in software written in imperative languages! So, in Haskell, computations are kept in IO.

I hope this dispels at least some of your confusion.

这篇关于关于Haskell IO Monad中的(何时执行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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