新手:了解主要和IO() [英] Newbie: understanding main and IO()
问题描述
I / O操作的特殊之处在于,如果它们属于主函数,就会执行。
但在下面的例子中,'greet'永远不会返回,因此不应该打印任何内容。
import Control.Monad
main =迎接
迎接=永远$ putStrLnHello World!
或者我应该问一下:落入主要功能意味着什么?首先, main
不是函数。首先, main
不是函数。它确实只是一个常规值,它的类型是 IO()
。该类型可以理解为:一个执行时产生()
类型的值的行为。
现在,运行时系统扮演了执行您所描述的操作的解释器的角色。让我们以你的程序为例:
main = forever(putStrLnHello world!)
请注意,我已经执行了转换。这是有效的,因为Haskell是一种引用透明的语言。运行时系统永久解决
并发现:
main = putStrLnHello world! >> MORE1
它还不知道 MORE1
是,但它现在知道它具有一个已知动作的组合,该动作将被执行。执行它后,它解决了第二个操作 MORE1
,并发现:
MORE1 = putStrLnHello world! >> MORE2
再次执行该构图中的第一个动作,然后继续解析。
当然这是高层次的描述。实际的代码不是解释器。但这是一种描绘Haskell程序如何执行的方式。再举一个例子:
main = forever(getLine>> = putStrLn)
RTS认为:
main =永远MORE1
<<永远解决>>
MORE1 = getLine>> = MORE2
<<执行getLine>>
MORE2结果= putStrLn结果>> MORE1
<<执行putStrLn结果(其中'结果'是行读取)
并重新开始>>
理解这个时,您明白了 IO String
不是带有副作用的字符串,而是描述可能产生字符串的动作。你也明白为什么懒惰对Haskell的I / O系统起作用是至关重要的。
While learning Haskell I am wondering when an IO action will be performed. In several places I found descriptions like this:
"What’s special about I/O actions is that if they fall into the main function, they are performed."
But in the following example, 'greet' never returns and therefore nothing should be printed.
import Control.Monad
main = greet
greet = forever $ putStrLn "Hello World!"
Or maybe I should ask: what does it mean to "fall into the main function"?
First of all, main
is not a function. It is indeed just a regular value and its type is IO ()
. The type can be read as: An action that, when performed, produces a value of type ()
.
Now the run-time system plays the role of an interpreter that performs the actions that you have described. Let's take your program as example:
main = forever (putStrLn "Hello world!")
Notice that I have performed a transformation. That one is valid, since Haskell is a referentially transparent language. The run-time system resolves the forever
and finds this:
main = putStrLn "Hello world!" >> MORE1
It doesn't yet know what MORE1
is, but it now knows that it has a composition with one known action, which is executed. After executing it, it resolves the second action, MORE1
and finds:
MORE1 = putStrLn "Hello world!" >> MORE2
Again it executes the first action in that composition and then keeps on resolving.
Of course this is a high level description. The actual code is not an interpreter. But this is a way to picture how a Haskell program gets executed. Let's take another example:
main = forever (getLine >>= putStrLn)
The RTS sees this:
main = forever MORE1
<< resolving forever >>
MORE1 = getLine >>= MORE2
<< executing getLine >>
MORE2 result = putStrLn result >> MORE1
<< executing putStrLn result (where 'result' is the line read)
and starting over >>
When understanding this you understand how an IO String
is not "a string with side effects" but rather the description of an action that would produce a string. You also understand why laziness is crucial for Haskell's I/O system to work.
这篇关于新手:了解主要和IO()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!