这是 Haskell IO 的合理观点吗?

发布于 2025-01-08 05:31:31 字数 242 浏览 3 评论 0原文

这是 Haskell IO 的合理观点吗?

当给定一个程序时,Haskell 运行时会执行以下操作:

  1. 调用 main 来获取“IO 计算”,
  2. 然后执行或“运行”该计算,从而执行该计算包含的所有副作用。

这种两阶段方法允许 main 保持纯函数的状态。

在这种情况下,IO 计算就像具有显式排序的 Haskell 的特殊版本 - 或者也许有更好的方法来描述这一点?

Is this a reasonable view of Haskell IO?

When given a program, the Haskell runtime does the following:

  1. Calls main to get back an "IO computation"
  2. It then executes or "runs" that computation thereby performing all of the side-effects the computation contains.

This two-stage approach allows main to remain a pure function.

An IO computation in this case is like a special version of Haskell that has explicit sequencing - or perhaps there is a better way to describe this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2025-01-15 05:31:31

是的,这是程序执行方式的一个不错的语义模型。当然,实现并不是这样工作的,但您仍然可以使用该模型来推理程序。

但更一般地说,IO 的作用是允许您将命令式程序视为纯值。然后,Monad 操作允许您从较小的命令式程序(或在此上下文中使用常用术语,操作)和纯函数组成命令式程序。因此,纯函数模型虽然无法执行命令式程序,但仍然可以将它们描述为 IO a 类型的表达式,并且编译器可以将这些描述转换为命令式代码。

或者你可以这样说:

  • 编译器(而不是运行时)评估main
  • 评估的结果是一项势在必行的计划。
  • 该程序被保存到目标可执行文件中。
  • 然后执行目标程序。

即,模型的“evaluate main”部分被推送到编译器,并且并不像您首先描述的那样位于运行时中。

Yeah, that's a decent semantic model of how the programs are executed. The implementation doesn't work like that, of course, but you can still use that model to reason about the programs.

But more generally, what IO does is to allow you to treat imperative programs as pure values. The Monad operations then allow you to compose imperative programs from smaller imperative programs (or to use the usual term in this context, actions) and pure functions. So the purely functional model, while it cannot execute imperative programs, can still describe them as expressions of type IO a, and the compiler can translate these descriptions into imperative code.

Or you could say this:

  • The compiler (not the runtime) evaluates main.
  • The result of that evaluation is an imperative program.
  • This program is saved to the target executable.
  • You then execute the target program.

I.e., the "evaluate main" part of your model is pushed to the compiler, and is not in the runtime as you first describe it.

旧故 2025-01-15 05:31:31

您对 IO 的看法很好,但我对这一行有疑问

调用 main 来获取“IO 计算”

思考 Haskell 的最佳方式是函数不执行任何操作。相反,您以声明方式描述什么是值。程序由名为 mainIO 值的描述组成。它“调用 main”的唯一意义是 main 的声明被简化为弱头范式(或类似的形式)。

IO 是任意副作用完全计算的类型。 Haskell 的纯子集是值的纯粹声明性描述,恰好允许不可判定的描述。将 Haskell 视为一种类似于集合论的数学语言。集合论中的陈述不执行任何操作,但它们可能涉及复杂的计算,例如“包含 Akerman's_function(30) 的最小集合”。它们还可以包含不可判定的语句,例如“S = 所有不包含自身的集合的集合”

@amindfv 说对了一半:main 不是“纯函数”。它根本不是一个函数。它是一个由纯约简定义的值,编码非纯计算。

Your view of IO is good, but I have a problem with this line

Calls main to get back an "IO computation"

The best way to think about Haskell is that functions dont do anything. Rather, you declaratively describe what values are. A program consists of a description of an IO value called main. The only sense to which it "calls main" is that the declaration of main is reduced to Weak Head Normal Form (or something similar).

IO is the type of arbitrary side effect-full computations. The pure subset of Haskell is a purely declarative description of values, that happens to allow for undecidable descriptions. Think of Haskell as a mathematical language like set theory. Statements in set theory dont do anything, but they might involve complicated computations like "the smallest set which contains Akerman's_function(30)". They can also contain undecidable statements like "S = the set of all sets that do not contain themselves"

@amindfv is half right: main is not a "pure function". It is not a function at all. It is a value, defined by a pure reduction, encoding unpure computation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文